leetcode 149. Max Points on a Line
来源:互联网 发布:线切割龙图案集编程 编辑:程序博客网 时间:2024/04/24 06:02
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Subscribe to see which companies asked this question
思路
给出平面上的几个点,求出同在一条直线上的点最多的个数
两个for循环,求两个点之间的斜率,注意垂直于x轴的斜率的表示以及覆盖点的个数[0,0]和[0,0]是两个点
class Point(object): def __init__(self,a=0,b=0): self.x=a self.y=bclass Solution(object): def maxPoints(self,points): Lenth=len(points) if not Lenth: return 0 Max_=0 for i in range(Lenth-1): dict={} count=0 for j in range(i+1,Lenth): if points[i].x-points[j].x==0: if points[i].y==points[j].y: count=count+1 dict.setdefault('b',0) else: if 'a' not in dict: dict.setdefault('a',1) else: dict['a']=dict['a']+1 else: k=(points[j].y-points[i].y)*1.0/(points[j].x-points[i].x) if k not in dict: dict.setdefault(k,1) else: dict[k]=dict[k]+1 #print dict Max_=max(Max_,max(dict.items(),key=lambda x:x[1])[1]+count) return (Max_+1)
0 0
- LeetCode 149. Max Points on a Line
- [leetcode] 149. Max Points on a Line
- leetcode 149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- leetcode.149. Max Points on a Line
- LeetCode-149.Max Points on a Line
- leetcode 149. Max Points on a Line
- leetCode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- 【LeetCode】149. Max Points on a Line
- IT资讯--------------制作首页
- libLAS1.8.0 编译和配置(VS2013+Win7 64)(二)
- 高质量代码开发总结
- 虚拟机联网--NAT模式
- STL 迭代器
- leetcode 149. Max Points on a Line
- Spring的xml配置文件的定义
- C++开发人脸性别识别教程(18)——辅助功能之文件名批量修改、方法验证
- android知识点总结
- 在虚拟机Ubuntu和宿主机Windows之间传送文件
- 关于AlphaTest
- 创建自定义hash列
- 类模板成员函数的定义、实现分离与error LNK2019的解决办法
- 深刻理解SVM