LeetCode#447 Number of Boomerangs

问题描述

Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).

Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).

Example:

Input:
[[0,0],[1,0],[2,0]]

Output:2

Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]

补充说明:

这个题目的要求大概是这个样子的:给定一个包含点坐标的list,最大长度为500,找到这个list中所有符合Boomerangs(回旋镖)规则的点的集合。这个单词我也没理解明白,但是根据提供的例子大概可以明白,意思就是生成的这个集合包含三个点i、j、k,点i到点j的距离等于点i到点k的距离。结果输出为给定这个列表中,满足这个条件的点集合的个数。

方案分析

  1. 看到这个题目,笔者第一反应就是想到了两点间的距离公式pow(y2-y1, 2) + pow(x2-x1, 2),毕竟初中数学。
  2. 知道如何判定了,就该想如何选点了,这里有个问题就是要选择三个点,自然而然想到了循环遍历,出于小聪明,笔者用python的情况下想到了itertools中的一个函数permutations,这个函数能从一个可迭代的类型对象中选择指定个数元素,生成tuple集合。是不是很机智。可是这是噩梦的开始,先上代码。

python实现

class Solution(object):
    def numberOfBoomerangs(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        nums = 0
        import itertools
        for point_tuple in itertools.permutations(points, 3):
            print point_tuple
            if pow((point_tuple[1][1] - point_tuple[0][1]),2)\
            + pow((point_tuple[1][0] - point_tuple[0][0]),2)\
            == pow((point_tuple[2][1] - point_tuple[0][1]),2)\
            + pow((point_tuple[2][0] - point_tuple[0][0]),2):
                nums += 1
        return nums

方案分析2

1.上面那个无外乎是在遍历所有情况,引用了高级函数一样不会避免枚举时间过长的问题,所以,上面方法不可行,原理上能讲明白,实际时间复杂度太高。

  1. ok,换个思路,使用hashmap,把里面所有的两点之间的距离计算一下,放到hashmap中,然后根据满足这个距离里面的点进行组合。这样就满足要求了。
  2. 讲解一下最后的那个res += pDict[p] * (pDict[p] - 1),这句话的意思是就是,当你发现两个点的距离为L时候,凡是满足L距离的点都存在了这个L为key的dict中,此时,你只需要从中任取3点,就能构成这个条件,因此,这样的组合有p×(p-1)对。

python实现2

class Solution(object):
    def numberOfBoomerangs(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        if len(points) < 3:
            return 0
        res = 0
        for i in range(len(points)):
            pDict = {}
            for j in range(len(points)):
                if j == i:
                    continue
                dis = pow(points[i][0] - points[j][0], 2) + pow(points[i][1] - points[j][1], 2)
                key = str(dis)
                if key in pDict:
                    pDict[key] += 1
                else:
                    pDict[key] = 1
            for p in pDict:
                if pDict[p] > 1:
                    res += pDict[p] * (pDict[p] - 1)
        return res
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,359评论 0 33
  • 题目 Given n points in the plane that are all pairwise dist...
    Eazow阅读 1,504评论 0 0
  • 动态规划(Dynamic Programming) 本文包括: 动态规划定义 状态转移方程 动态规划算法步骤 最长...
    廖少少阅读 8,881评论 0 18
  • 读一读 总会读到一篇美文 听一听 总会听到一首好歌 等一等总会等到相爱的人 笑一笑 总会看到生活的美好 幸福就在执...
    南方潇潇阅读 1,157评论 0 0
  • 每次谈到青春这个词,总有道不尽的话题。每个人的青春中都写满了各式各样的故事,毕业的别离,羞涩而单纯的爱情,在球场上...
    迷途囧爷阅读 7,722评论 0 2

友情链接更多精彩内容