LeetCode 447. Number of Boomerangs

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;
    }
};
posted @ 2018-12-04 21:17  A-Little-Nut  阅读(118)  评论(0编辑  收藏  举报