Skip to content

Commit 7fa2975

Browse files
author
lightmen
committed
add number-of-boomerangs.py
1 parent 59904cf commit 7fa2975

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def dist(self, x1, y1, x2, y2):
3+
return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)
4+
5+
def numberOfBoomerangs(self, points):
6+
"""
7+
:type points: List[List[int]]
8+
:rtype: int
9+
"""
10+
ret = 0
11+
for i in range(len(points)):
12+
d = {}
13+
for j in range(len(points)):
14+
if i == j:
15+
continue
16+
key = self.dist(points[i][0], points[i][1], \
17+
points[j][0], points[j][1])
18+
ret += (2 * d.get(key,0))
19+
d[key] = d.get(key, 0) + 1
20+
21+
return ret

0 commit comments

Comments
 (0)