LeetCode 447. Number of Boomerangs

Posted A-Little-Nut

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
    }
};

以上是关于LeetCode 447. Number of Boomerangs的主要内容,如果未能解决你的问题,请参考以下文章

leetcode447-Number of Boomerangs

[LeetCode]447 Number of Boomerangs

leetcode 447 Number of Boomerangs

Leetcode 447. Number of Boomerangs JAVA语言

LeetCode 447. Number of Boomerangs; 149. Max Points on a Line

leetcode?pythonSum Of Two Number