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 iand 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]]
class Solution(object):
def numberOfBoomerangs(self, points):
p = points
res = 0
for i in xrange(len(p)):
dic = {}
for j in xrange(len(p)):
dist = (p[j][1] - p[i][1])**2 + (p[j][0] - p[i][0])**2
dic[dist] = dic.get(dist,0) +1
#print dic
for i in dic.values():
if i > 1:
res += i*(i-1)
return res
362




被折叠的 条评论
为什么被折叠?



