题目

给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 ij 之间的距离和 ik 之间的距离相等(需要考虑元组的顺序)。
找到所有回旋镖的数量。你可以假设 n 最大为 500,所有点的坐标在闭区间 [-10000, 10000] 中。
示例:
输入:
[[0,0],[1,0],[2,0]]

输出:
2

解释:
两个回旋镖为 **[[1,0],[0,0],[2,0]]****[[1,0],[2,0],[0,0]]**

方案一

  1. class Solution:
  2. def numberOfBoomerangs(self, points: List[List[int]]) -> int:
  3. res = 0
  4. for i in range(len(points)):
  5. d = {} # key 为与 points[i] 的距离,value 为距离相等的个数
  6. for j in range(len(points)):
  7. if i == j: continue
  8. # 计算距离时不需要开方,防止精度问题
  9. dis = (points[i][0] - points[j][0]) ** 2 + (points[i][1] - points[j][1]) ** 2
  10. if dis not in d:
  11. d[dis] = 0
  12. d[dis] += 1
  13. for num in d.values():
  14. if num > 1:
  15. res += num * (num - 1)
  16. return res