# 149. Max Points on a Line - 草稿

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

```from fractions import Fraction
from collections import defaultdict
import numpy as np
d = defaultdict(lambda: 100)
class Solution(object):
def isequalpt(self, p1, p2):
if p1.x == p2.x and p1.y == p2.y:
return True
else:
return False
def maxPointsatPoint(self, points):
pt = points[0]
del points[0]
i = 0
maxnum = 0
same = 1
dic = defaultdict(lambda: 0)
while i < len(points):
p = points[i]
if self.isequalpt(pt, p):
same += 1
# 并且把p删除
points.remove(p)
continue
if pt.x != p.x:
k = (p.y-pt.y)*np.longdouble(1)/(p.x-pt.x)
dic[k] += 1
maxnum = max(maxnum, dic[k])
else:
dic['zero'] += 1
maxnum = max(maxnum, dic['zero'])
i += 1
maxnum += same
return maxnum

def maxPoints(self, points):
"""
:type points: List[Point]
:rtype: int
"""
lens = len(points)
print lens
if lens < 3:
return lens
self.dic=None
self.maxnumAll = 0
i = 0
while lens > 2:
lr = self.maxPointsatPoint(points)
self.maxnumAll = max(self.maxnumAll, lr)
lens = len(points)

return self.maxnumAll```

0 条评论

## 相关文章

2.3K50

16220

### 关于Dapper.NET的相关论述

年少时，为何不为自己的梦想去拼搏一次呢？纵使头破血流，也不悔有那年少轻狂。感慨很多，最近事情也很多，博客也很少更新了，毕竟每个人都需要为自己的生活去努力。...

31270

49620

### Netty NioEventLoop源码解读

NioEventLoop中维护了一个线程，线程启动时会调用NioEventLoop的run方法，执行I/O任务和非I/O任务：I/O任务：即selectio...

26030

22070

1.4K60

### Cortex-M3启动深度解析

Cortex-Mx启动，备忘，以免将来忘记。 中断向量表不用说，从重置中断开始吧 LDR R0, =SystemInit BLX R0 LDR ...

20960

73080

1.6K50