码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 447. Number of Boomerangs

时间:2018-12-04 22:40:17      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:etc   group   tween   clu   tin   lan   hat   air   +=   

Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between iand j equals the distance between i and k (the order of the tuple matters).

Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).

Example:

[[0,0],[1,0],[2,0]]

Output:
2

Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
class Solution {
public:
    int numberOfBoomerangs(vector<pair<int, int>>& points) {//用map来记录到某个点为某个距离点的个数
        int res=0;
        for(int i=0; i<points.size(); i++){
            unordered_map<long,int> group(points.size());
            for(int j=0; j<points.size(); j++){
                if(i==j) continue;
                int p=(points[i].first-points[j].first)*(points[i].first-points[j].first);
                int q=(points[i].second-points[j].second)*(points[i].second-points[j].second);
                group[p+q]++;
            }
            for(auto o:group)
                if(o.second>1)
                   res+=o.second*(o.second-1);
        }
        return res;
    }
};

LeetCode 447. Number of Boomerangs

标签:etc   group   tween   clu   tin   lan   hat   air   +=   

原文地址:https://www.cnblogs.com/A-Little-Nut/p/10066947.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!