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. NSUserDefaults:熟悉与陌生(转)

    转载自:http://swiftcafe.io/2016/04/04/nsuserdefaults/?hmsr=toutiao.io&utm_medium=toutiao.io&utm ...

  2. 理解Cookie和Session机制(转)

    目录[-] Cookie机制 什么是Cookie 记录用户访问次数 Cookie的不可跨域名性 Unicode编码:保存中文 BASE64编码:保存二进制图片 设置Cookie的所有属性 Cookie ...

  3. 软件打包为exe NSIS单文件封包工具V2.3

    NSIS单文件封包工具V2.3 这是一款基于NSIS模块的封包制作工具,lzma算法最大压缩率,支持制作单文件,以及NSIS自定义解压封包. 支持注册dll,exe,reg,bat文件 默认提取设置程 ...

  4. 菜鸟初识python request属性及方法说明

    if  request.REQUEST.has_key('键值'): HttpRequest对象的属性 参考: 表 H-1. HttpRequest对象的属性 属性 描述 path 表示提交请求页面完 ...

  5. shiro登陆后没有返回设置的successUrl?

    第一次学习shiro的时候,并没有发现很大的问题.但后来在做项目的时候,特别是当访问的url是iframe的页面的时候,session又过期了,跳转到登陆页,完成登陆操作后,返回了只有iframe的页 ...

  6. 【bzoj1227】 SDOI2009—虔诚的墓主人

    http://www.lydsy.com/JudgeOnline/problem.php?id=1227 (题目链接) 题意 一个n*m的公墓,一个点上要么是墓地,要么是常青树,给出一个数K,并规定每 ...

  7. python作用域和多继承

    python作用域 python无块级作用域 看c语言代码: #include<stdio.h> int main() { > ) { ; } printf("i = %d ...

  8. javascript面向对象:继承、多态

    继承 js中同样可以实现类的继承这一面向对象特性,继承父类中的所有成员(变量和属性),同时可扩展自己的成员,下面介绍几种js中实现继承的方式: 1,对象模仿:通过动态的改变 this 指针的指向,实现 ...

  9. 7 款顶级开源 BI(商务智能)软件和报表工具

    在这个信息化时代,每分每秒都产生海量数据.在海量数据中,挖掘出有用的数据,并且能以较人性化.直观的方式展示这些数据,变得尤为重要.本文将介绍 7款顶级开源 BI(商务智能)软件和报表工具,用于商业数据 ...

  10. 大熊君大话NodeJS之------MongoDB模块(额外篇)

    一,开篇分析 这篇属于扩展知识篇,因为在下面的文章中会用到数据库操作,所以今天就来说说它(Mongodb模块). (1),简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为 ...