Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
number-of-boomerangs.py 1.13 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-10-13 01:56 +08:00 . add complexity
# 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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助