447. Number of Boomerangs
阿新 • • 發佈:2018-12-11
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]]
思路 點之間距離相同,從中選3個,一個排列組合問題。 可以放入hash,map中儲存,距離相同的點的數目,然後計算。
code:
class Solution { public: int numberOfBoomerangs(vector<pair<int, int>>& points) { int count=0; int size=points.size(); for(auto p:points){ unordered_map<double,int>m; for(auto p1:points){ m[hypot(p.first-p1.first,p.second-p1.second)]++; } for(auto v:m){ if(v.second>1){ count+=v.second*(v.second-1); } } } return count; } };
三角函式 hypot 使用 auto遍歷