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.

解题思路:

1.首先由这么一个O(n^3)的方法,也就是算出每条线的方程(n^2),然后判断有多少点在每条线上(N)。这个方法肯定是可行的,只是复杂度太高
2.然后想到一个O(N)的,对每一个点,分别计算这个点和其他所有点构成的斜率,具有相同斜率最多的点所构成的直线,就是具有最多点的直线。

注意的地方:

1.重合的点
2.斜率不存在的点

 # Definition for a point
class Point:
def __init__(self, a=0, b=0):
self.x = a
self.y = b class Solution:
# @param points, a list of Points
# @return an integer
def calcK(self,pa, pb):
t = ((pb.y - pa.y) * 1.0) / (pb.x - pa.x)
return t def maxPoints(self, points):
l = len(points)
res = 0
if l <= 2:
return l
for i in xrange(l):
same = 0
k = {}
k['inf'] = 0
for j in xrange(l):
if points[j].x == points[i].x and points[j].y != points[i].y:
k['inf'] += 1
elif points[j].x == points[i].x and points[j].y == points[i].y:
same +=1
else:
t = self.calcK(points[j],points[i])
if t not in k.keys():
k[t] = 1
else:
k[t] += 1
res = max(res, max(k.values())+same)
return res def main():
points = []
points.append(Point(0,0))
points.append(Point(1,1))
points.append(Point(1,-1))
#points.append(Point(0,0))
#points.append(Point(1,1))
#points.append(Point(0,0))
s = Solution()
print s.maxPoints(points) if __name__ == '__main__':
main()

【leetcode】Max Points on a Line的更多相关文章

  1. 【leetcode】Max Points on a Line(hard)☆

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

  2. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  3. [leetcode]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. ...

  4. 【LeetCode OJ】Max Points on a Line

    Problem: Given n points on a 2D plane, find the maximum number of points that lie on the same straig ...

  5. 【leetcode刷题笔记】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. ...

  6. [LeetCode] 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. ...

  7. Java for LeetCode 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. ...

  8. leetcode 149. Max Points on a Line --------- java

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

  9. [LeetCode OJ] 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.

    //定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...

随机推荐

  1. Apache与Tomcat服务器

    Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一.在Apache基金会里面Apache S ...

  2. WinForm窗体代码结构优化

    选择系统新建WinForm程序,会生成FormMain.cs/ FormMain.Designer.cs/ FormMain.resx 当我们发现系统生成的FormMain.cs里面代码太多的时候,不 ...

  3. 创建一个自定义颜色IRgbColor

    后续文章需要用到,很简单的一个小函数 /// <summary> /// 自定义颜色 /// </summary> /// <param name="r&quo ...

  4. Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数、ColModel API、事件及方法

    系列索引 Web jquery表格组件 JQGrid 的使用 - 从入门到精通 开篇及索引 Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数.ColModel API.事件 ...

  5. [Unity3D]粒子系统学习笔记

    粒子阴影的处理 通过Material填充粒子系统的render后,默认是显示阴影的: 可以通过设置来调整: 调整后的效果, 每个粒子就没有阴影了 增加粒子效果 设置为合成的材质,效果显示加倍: 添加子 ...

  6. linux rpm安装apache php mysql

    CentOS 可以通过 yum 安装: yum -y install httpd php php-mysql  mysql-serverservice httpd status|start|stop| ...

  7. Tomcat基本入门知识及发布,虚拟访问及启动碰到的错误,虚拟目录,虚拟路径,各种Tomcat的配置

    Tomcat容器入门介绍 转自javaresearch.com由timgball 整理 Tomcat是一个免费的开源Web服务器,最新版本是5.5.1,支持Servlet2.4,JSP2.0,非常适合 ...

  8. [NHibernate]NHibernate.Tool.hbm2net

    系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate ...

  9. 【06-18】CentOS使用笔记

    使用中文输入法 搜狗输入法只支持Ubuntu sudo yum install "@Chinese Support" [系统]--->[首选项]--->[输入法]--& ...

  10. 关于Hibernate5.x的那点事

    1.如果采用程序建表: 4.x版本: Configuration cfg = new Configuration().configure(); SchemaExport se = new Schema ...