您的位置:首页 > 其它

LeetCode : Number of Boomerangs

2017-03-05 21:07 176 查看
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 i and 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:

Input:

[[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) {
int ans = 0, len = points.size();
for(int p1=0;p1<points.size();++p1)
{
unordered_map<double, int> hash;
for(int p2=0;p2<points.size();p2++)
{
//hash[hypot(p1.first-p2.first, p1.second-p2.second)]++;
int dx = points[p1].first-points[p2].first;
int dy = points[p1].second-points[p2].second;
hash[dx*dx+dy*dy]++;

}
for(auto val:hash)
{
if(val.second>1)
ans+=val.second*(val.second-1);
}
}
return ans;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: