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]]
Subscribe to see which companies asked this question
有三个问题
第一个,最开始的时候还去计算了开平方,但实际上并没有用,而且竟然还用了numpy,实在是不开窍
第二个,最开始的时候虽然结果对,但是一直超时,或者超内存,看了一眼网上的结果,发现,其实并不需要在外面弄一个打的dic,每个循环一个dic就行了
第三个,每个循环,每个节点与其他节点比较就行了
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