给定平面中所有成对的不同点的n个点,一个“回旋镖”是一个点(i,j,k)的元组,使得i和j之间的距离等于i和k之间的距离。找到回旋镖的数量。
题目的难点在于发现‘对于每一个点,有k个距离为d的点,就有k *(k-1)种组合’
举例说明,比如有一个点a,和它距离都是1的点有b,c,d,那么一共的组合就有6种,包括:[a, b, c], [a, c, b], [a, b, d], [a, d, b], [a, c, d], [a, d, c]
class Solution(object):
def numberOfBoomerangs(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
ans = 0
for x1,y1 in points:
Dict = collections.defaultdict(int)
for x2,y2 in points:
Dict[(x1-x2)**2 + (y1-y2)**2] += 1
for d in Dict:
ans += Dict[d] * (Dict[d]-1)
return ans