8.10 - hard - 34

149. Max Points on a Line
这道题的想法还是挺简单的,只是要分清三种情况,点的横坐标相同,点重合,其它情况。

# Definition for a point.
# class Point(object):
#     def __init__(self, a=0, b=0):
#         self.x = a
#         self.y = b
import numpy as np
class Solution(object):
    def maxPoints(self, points):
        """
        :type points: List[Point]
        :rtype: int
        """
        if not points:
            return 0
        if len(points) <= 2:
            return len(points)
        # 想法很简单就是求每两点间的slope
        res = 0 
        for p in points:
            m = {}
            duplicate = 1
            vertical = 0
            for p1 in points:
                if p != p1:
                    if p.x == p1.x and p.y == p1.y:
                        duplicate += 1
                    elif p.x == p1.x:
                        vertical += 1
                    else:
                        slope = (p1.y - p.y) * np.longdouble(1) / (p1.x - p.x)
                        m[slope] = m.get(slope, 0) + 1
            print m
            res = max(res, vertical+duplicate)
            if m:
                res = max(res, max(m.values())+duplicate)
        return res
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,771评论 0 33
  • 这是所有类型里我觉得最有趣的一个类型,哈哈。来被虐一下。 258. Add Digits 数字根的性质: 任何数字...
    __小赤佬__阅读 403评论 0 0
  • 18- UIBezierPath官方API中文翻译(待校对) ----------------- 华丽的分割线 -...
    醉卧栏杆听雨声阅读 1,084评论 1 1
  • 恋爱十年迎来皮皮(小名),一路上坎坎坷坷吵吵闹闹分分合合~~ ……中间恋爱史省略一万字,2014年我们结婚了,我经...
    谭雪琼阅读 346评论 0 1
  • 今夜有点失眠,只是有点想你,想念一个未曾谋面的人…
    不曾离去阅读 321评论 0 0