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:
1 2 3 4 5 6 7 8
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]]
classSolution(object): defnumberOfBoomerangs(self, points): """ :type points: List[List[int]] :rtype: int """ res = 0 for x1,y1 in points: dis_map = {} for x2,y2 in points: dis_map[(x1-x2)**2 + (y1-y2)**2] = 1 + dis_map.get((x1-x2)**2 + (y1-y2)**2,0) for dis in dis_map: #1 res += dis_map[dis] * (dis_map[dis] - 1) return res
classSolution(object): defnumberOfBoomerangs(self, points): """ :type points: List[List[int]] :rtype: int """ count = 0 for point1 in points: m = {} for point2 in points: dx = point1[0] - point2[0] dy = point1[1] - point2[1] d = dx*dx + dy*dy if d in m: count += m[d]*2 m[d] +=1 else: m[d] = 1 return count