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