447. Number of Boomerangs python

给定平面中所有成对的不同点的n个点,一个“回旋镖”是一个点(i,j,k)的元组,使得i和j之间的距离等于i和k之间的距离。找到回旋镖的数量。

题目的难点在于发现‘对于每一个点,有k个距离为d的点,就有k *(k-1)种组合’

举例说明,比如有一个点a,和它距离都是1的点有b,c,d,那么一共的组合就有6种,包括:[a, b, c], [a, c, b], [a, b, d], [a, d, b], [a, c, d], [a, d, c]

class Solution(object):
    def numberOfBoomerangs(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        ans = 0
        for x1,y1 in points:
            Dict = collections.defaultdict(int)
            for x2,y2 in points:
                Dict[(x1-x2)**2 + (y1-y2)**2] += 1
            for d in Dict:
                    ans += Dict[d] * (Dict[d]-1)
        return ans


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值