if len(points) <= 2: return len(points) maxans = 0 for i in points: same = sum(1for j in points if j == i) hashmap = Counter([K(i,j) for j in points if j != i]) tempmax = hashmap.most_common(1)[0][1] if hashmap else0 maxans = max(same + tempmax, maxans) return maxans
一行代码
其实就是分步代码利用生成器构造一下
1 2 3 4
from collections import Counter classSolution: defmaxPoints(self, points: List[List[int]]) -> int: return max(sum(1for j in points if j == i) + Counter([float('Inf') if i[1] - j[1] == 0else (i[0] - j[0]) / (i[1] - j[1]) for j in points if j != i]).most_common(1)[0][1] if sum(1for j in points if j == i) != len(points) else sum(1for j in points if j == i) for i in points) if len(points) > 2else len(points)