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. <<< html编码中js和html编码不一致导致乱码

    在html中,有时把编码设置成UTF-8之后,引入js,页面不会有乱码,但是有关js的东西会出现乱码, 大概问题就是js默认编码不是UTF-8, 解决办法:将js文件用记事本打开,在另存为,保存的时候 ...

  2. ypzl药品质量不合格数据库-excel自动排版

    原创: qq:231469242 import xlrdimport pandas,numpyimport matplotlib.pyplot as pltimport pandas as pd #参 ...

  3. Redis主-从部署实践

    0. 前言 这篇文章简要介绍Redis的主从部署,实现了一主二从,使用两个哨兵监控,以实现简单的HA,其中从库作为备机. 1. 部署 这里有三台服务器,其中239主机上的Redis作为主库,其余两个作 ...

  4. 如何刷新或清除HttpURLConnection的连接缓存

    项目需要定期与远程服务器同步数据,基于如下代码: URL url = new URL("http://test.com/sales/info"); connection = (Ht ...

  5. MySql 杂记

    1:声明一个int变量时,设置它默认为0,而不是空或null. int 型,取值范围-2,147,483,648 到 2,147,483,647 ,默认值是 0 int是值类型,读内存区间中指定长度单 ...

  6. HSF和Dubbo有什么区别

    一. 以下摘录自企业级分布式应用服务EDAS官网段落 RPC服务 提供对Dubbo和HSF两个RPC框架的支持.阿里巴巴第一代RPC框架Dubbo是国内第一款成熟的商用级RPC框架,已于2011年正式 ...

  7. [Python] Python学习笔记之常用模块总结[持续更新...]

    作为一种极其简单的编程语言,Python目前成为了最炙手可热的几种语言之一.它不仅简单易学,而且它还为用户提供了各种各样的模块,功能强大,无所不能.有利必有弊,学习Python同样有困扰,其中之一就是 ...

  8. PHP变量入门教程(3)global 关键字

    global关键字 首先,一个使用 global 的例子: 使用 global <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b ...

  9. sqlplus连接oracle失败分析和解决

    背景: 多台Linux服务器需要安装Oracle客户端,实现和Oracle数据库连接做业务处理. 安装完第一台后,直接将安装的目录压缩并复制到其他几台机器上,启动sqlplus连接数据库时,一直提示输 ...

  10. 计算 TP90TP99TP...

    what-do-we-mean-by-top-percentile-or-tp-based-latency tp90 is a minimum time under which 90% of requ ...