难度困难
给你一个数组 points
,其中 points[i] = [xi, yi]
表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。
示例 1:
输入:points = [[1,1],[2,2],[3,3]] 输出:3
示例 2:
输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 输出:4
class Solution: def maxPoints(self, points: List[List[int]]) -> int: n = len(points) cnt = 0 res = 1 for i in range(n): pa = points[i] for j in range(i+1,n): pb = points[j] cnt = 2 for k in range(j+1,n): pc = points[k] # 直线线的两点式方程(y-y1)/(x-x1)=(y-y2)/(x-x2),再对角相乘就可以了 s1 = (pc[1]-pa[1]) * (pc[0]-pb[0]) s2 = (pc[0]-pa[0]) * (pc[1]-pb[1]) if s1==s2: cnt+=1 res = max(res,cnt) return res
标签:直线,cnt,res,pa,149,pb,pc,points,点数 From: https://www.cnblogs.com/zle1992/p/17519902.html