Python | Leetcode Python题解之第149题直线上最多的点数

题目:

题解:

class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
        n = len(points)
        if n <= 2:
            return n
        res = 2
        for i in range(n):
            x1, y1 = points[i][0], points[i][1]
            has = {}
            for j in range(i + 1, n):
                x2, y2 = points[j][0], points[j][1]
                if x1 == x2:
                    a, b, c = 1, 0, -x1
                elif y1 == y2:
                    a, b, c = 0, 1, -y1
                else:
                    a = 1.0
                    b = 1.0 * (x1 - x2) / (y2 - y1)
                    c = 1.0 * (x1 * y2 - x2 * y1) / (y1 - y2)
                if (a,b,c) in has.keys():
                    has[(a,b,c)]+=1
                    res = max(res,has[(a,b,c)])
                else:
                    has[(a,b,c)] = 2
        return res

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-15 23:44:01       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-15 23:44:01       97 阅读
  3. 在Django里面运行非项目文件

    2024-06-15 23:44:01       78 阅读
  4. Python语言-面向对象

    2024-06-15 23:44:01       88 阅读

热门阅读

  1. Html_Css问答集(2)

    2024-06-15 23:44:01       28 阅读
  2. 在面试中展示自己的系统架构设计能力

    2024-06-15 23:44:01       24 阅读
  3. 网络安全练气篇——OWASP TOP 10

    2024-06-15 23:44:01       31 阅读
  4. 什么是js防抖节流?

    2024-06-15 23:44:01       27 阅读
  5. Spring框架的原理及应用详解(二)

    2024-06-15 23:44:01       28 阅读
  6. 2024年6月13日随笔

    2024-06-15 23:44:01       31 阅读
  7. 前端-高德地图去掉左下角Logo和版本号

    2024-06-15 23:44:01       26 阅读
  8. 【python】正则匹配国内手机号

    2024-06-15 23:44:01       25 阅读
  9. VUE 查询条件重置之后, 子组件数据未置空

    2024-06-15 23:44:01       103 阅读
  10. 基于SpringCloudAlibaba的微服务架构设计模式

    2024-06-15 23:44:01       31 阅读