美文网首页
回旋镖的数量

回旋镖的数量

作者: MrHitchcock | 来源:发表于2020-04-05 10:17 被阅读0次
  • 题目描述:给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i 和 k 之间的距离相等(需要考虑元组的顺序)。

    • 找到所有回旋镖的数量。你可以假设 n 最大为 500,所有点的坐标在闭区间 [-10000, 10000] 中。

    • 示例:

      输入:
      [[0,0],[1,0],[2,0]]
      输出:
      2
      解释:
      两个回旋镖为 [[1,0],[0,0],[2,0]] 和 [[1,0],[2,0],[0,0]]

    • 链接:https://leetcode-cn.com/problems/number-of-boomerangs


  • 解题思路:
    1.确定第一个点,然后计算与其他点的距离
    2.如果距离相等的次数大于等于2,则使用排列组合解决 perm(次数,2)

  • Python版:

from collections import Counter
from scipy.special import perm

class Solution:
    def numberOfBoomerangs(self, points: List[List[int]]) -> int:
        count = 0
        for point in points:
            
            # distance2 记录对这个点来说的所有的距离
            distance2 = []
            for neighbor in points:
                distance2.append((point[0] - neighbor[0]) ** 2 + (point[1] - neighbor[1]) ** 2)
            
            frequency = Counter(distance2)
            
            for dist,num in frequency.items():
                if num >= 2:
                    count += perm(num,2)
                    
        return int(count)

相关文章

  • 回旋镖的数量

    题目描述 给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的...

  • 回旋镖的数量

    题目描述:给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的...

  • LeetCode 查找表专题 6:灵活选择键值:Number o

    例题:LeetCode 第 477 题:回旋镖的数量 传送门:447. 回旋镖的数量。 给定平面上 n 对不同的点...

  • 447. 回旋镖的数量

    给定平面上 n 对 互不相同 的点 points ,其中 points[i] = [xi, yi] 。回旋镖 是由...

  • 447. 回旋镖的数量

    2021-09-13 LeetCode每日一题 链接:https://leetcode-cn.com/proble...

  • 1.数据结构-字典(哈希表)

    2. 447. 回旋镖的数量[https://leetcode-cn.com/problems/number-of...

  • T447、回旋镖数量

    给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离和 i...

  • Leetcode 447. 回旋镖的数量

    题目描述 给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的...

  • 【数组】447. 回旋镖的数量

    题目 给定平面上 n 对不同的点,“回旋镖” 是由点表示的元组 (i, j, k) ,其中 i 和 j 之间的距离...

  • 回旋镖

    心里有点堵,自己一直这么信任,这么支持的人,原来就是在背后传言我的人!承认我非贤圣,乍一听到,心里那种感觉...

网友评论

      本文标题:回旋镖的数量

      本文链接:https://www.haomeiwen.com/subject/gtiyphtx.html