当前位置:   article > 正文

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

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

题目:

题解:

  1. class Solution:
  2. def maxPoints(self, points: List[List[int]]) -> int:
  3. n = len(points)
  4. if n <= 2:
  5. return n
  6. res = 2
  7. for i in range(n):
  8. x1, y1 = points[i][0], points[i][1]
  9. has = {}
  10. for j in range(i + 1, n):
  11. x2, y2 = points[j][0], points[j][1]
  12. if x1 == x2:
  13. a, b, c = 1, 0, -x1
  14. elif y1 == y2:
  15. a, b, c = 0, 1, -y1
  16. else:
  17. a = 1.0
  18. b = 1.0 * (x1 - x2) / (y2 - y1)
  19. c = 1.0 * (x1 * y2 - x2 * y1) / (y1 - y2)
  20. if (a,b,c) in has.keys():
  21. has[(a,b,c)]+=1
  22. res = max(res,has[(a,b,c)])
  23. else:
  24. has[(a,b,c)] = 2
  25. return res
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号