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]]
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; } };
相关文章推荐
- Leetcode: Number of Boomerangs
- [Leetcode] Number of 1 Bits
- LeetCode -- Number of Islands
- LeetCode-Hash-447. Number of Boomerangs
- LeetCode 第 191 题 (Number of 1 Bits)
- leetcode oj java Number of Boomerangs
- leetcode-191-Number of 1 Bits
- LeetCode191—Number of 1 Bits
- 【leetcode】【191】Number of 1 Bits
- LeetCode Number of Boomerangs
- [LeetCode]Number of 1 Bits
- [leetcode][bit] Number of 1 Bits
- leetcode 200: Number of Islands
- LeetCode题解:Number of 1 Bits
- LeetCode 191:Number of 1 Bits
- Leetcode算法学习日志-447 Number of Boomerangs
- Leetcode Problem.191—Number of 1 Bits
- Leetcode--Number of 1 Bits
- [leetcode 191]Number of 1 Bits
- LeetCode447. Number of Boomerangs