leetcode 149. Max Points on a Line
2016-02-29 20:15
405 查看
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]是两个点
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=b class 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)
相关文章推荐
- 高质量代码开发总结
- iOS: 学习笔记, Swift操作符定义
- 动态规划算法经典案例
- libLAS1.8.0 编译和配置(VS2013+Win7 64)(二)
- thinkphp3.2架构及源码理解
- win8.1与谷歌浏览器快捷键使用(加快开发速度)
- bootstrap插件地址
- CentOS6.5下解压文件.tar.gz .war .zip
- java中值类型和引用类型的区别
- 《Cocos2d-x游戏开发实战精解》学习笔记2--在Cocos2d-x中显示一行文字
- AnjularJS 指令
- swift 修改命名空间
- CentOS 6.5下本地yum源与网络yum源的配置使用
- PigeonCall:一款Android VoIP网络电话App架构分析
- 1176: [Balkan2007]Mokia/2683: 简单题 CDQ分治+树状数组
- css练习——图书网站(head)
- 【LeetCode】89. Gray Code
- 【POJ1741】Tree【点分治】
- 设计模式--工厂设计模式
- Android手机两种方式获取IP地址