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个点,计算1个点到另外两个点都相等有多少种组合
hypot(x,y) 计算x
与y
平方和的平方根
C++(386ms):
1 class Solution { 2 public: 3 int numberOfBoomerangs(vector<pair<int, int>>& points) { 4 int res = 0; 5 unordered_map<double, int> ctr(points.size()); 6 for (auto p : points) { 7 for (auto q : points){ 8 if (p == q) 9 continue ; 10 double t = hypot(p.first - q.first, p.second - q.second) ; 11 ctr[t]++ ; 12 res += 2 * (ctr[t]-1); 13 } 14 ctr.clear() ; 15 } 16 return res; 17 } 18 };