代码拉取完成,页面将自动刷新
# Time: O(n^2)
# Space: O(n)
import collections
class Solution(object):
def numberOfBoomerangs(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
result = 0
for i in xrange(len(points)):
group = collections.defaultdict(int)
for j in xrange(len(points)):
if j == i:
continue
dx, dy = points[i][0] - points[j][0], points[i][1] - points[j][1]
group[dx**2 + dy**2] += 1
for _, v in group.iteritems():
if v > 1:
result += v * (v-1)
return result
def numberOfBoomerangs2(self, points):
"""
:type points: List[List[int]]
:rtype: int
"""
cnt = 0
for a, i in enumerate(points):
dis_list = []
for b, k in enumerate(points[:a] + points[a + 1:]):
dis_list.append((k[0] - i[0]) ** 2 + (k[1] - i[1]) ** 2)
for z in collections.Counter(dis_list).values():
if z > 1:
cnt += z * (z - 1)
return cnt
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。