题目 https://oj.leetcode.com/problems/max-points-on-a-line/

答案与分析 http://www.aiweibang.com/yuedu/183264.html

     http://blog.csdn.net/ojshilu/article/details/21618675

     http://www.1point3acres.com/bbs/thread-14425-1-1.html

    http://akalius.iteye.com/blog/161852

      http://jchu.blog.sohu.com/116744856.html

       https://oj.leetcode.com/discuss/7607/accepted-code-in-python

下面是我的解法,跟http://blog.csdn.net/ojshilu/article/details/21618675 的解法是类似的,

主要思想是:1)选去平面上任意的两个点 2) 计算剩下的点会在之前两点决定的线上会有多少个3)记录最大值

我自己试了试少量的点,算法上是正确的,但是提交到leetcode就TLE(Time Limit Exceeded)了,但是上面那个链接上的用的是c++就通过了。。。。

那就只好学习下leetcode上AC的python代码了(感觉还是国内博客上的代码好懂。。。)

这个是我自己的代码

# 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 maxPoints(self, points): max_num = 2 if len(points)<3:
return len(points) for a in points:
for b in points[1:]:
sums = 2
if b.x == a.x and b.y == a.y:
continue for c in points:
if c.x !=a.x and c.x != b.x\
and c.y!=a.y and c.y != b.y\
and (c.y-a.y)*(b.x-a.x) == (b.y-a.y)*(c.x-a.x):#(c.y-a.y)/(c.x-a.x) == (b.y-a.y)/(b.x-a.x)
sums = sums +1 if max_num < sums :
max_num =sums return max_num points = []
points.append(Point(40,-20))
points.append(Point(4,-2))
points.append(Point(8,-4))
points.append(Point(50,-17)) print points c = Solution()
a = c.maxPoints(points) print a

leetcode ex3 找出穿过最多点的直线 Max Points on a Line的更多相关文章

  1. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  2. LeetCode: Max Points on a Line 解题报告

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  3. LeetCode 5071. 找出所有行中最小公共元素(Java)

    题目:5071. 找出所有行中最小公共元素 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1 ...

  4. Java实现 LeetCode 719 找出第 k 小的距离对(二分搜索法+二分猜数字)

    719. 找出第 k 小的距离对 给定一个整数数组,返回所有数对之间的第 k 个最小距离.一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值. 示例 1: 输入: nums = [1,3, ...

  5. [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. ...

  6. 【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. ...

  7. LeetCode之Max Points on a Line Total

    1.问题描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

  8. 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 l ...

  9. LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game

    地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...

随机推荐

  1. [UE4]扔枪后捡枪:Get Overlapping Actors

    一.搜索碰撞体是否碰撞到枪,使用Get Overlapping Actors,只搜索Class Filter设置的对象类型.Get Overlapping Actors没有Class Filter参数 ...

  2. sklearn不同数量的训练集在测试集上的表现的曲线刻画

    def plot_learning_curve(estimator,X,y,cv=5,train_sizes=[0.1,0.3,0.5,0.7,0.8,0.9]): """ ...

  3. 关于-RegExp

    // exec() exec() 查找并返回当前的匹配结果,并以数组的形式返回. // test() test() 方法检索字符串中的指定值.返回值是 true 或 false. // compile ...

  4. Android收发短信

    效果:点击发送短信开始发送短信 收到短信时将短信的内容显示出来 代码如下: 一.权限声明 <uses-permission android:name="android.permissi ...

  5. mysql中min和max查询优化

    mysql max() 函数的需扫描where条件过滤后的所有行: 在测试环境中重现: 测试版本:Server version:         5.1.58-log MySQL Community ...

  6. 网络基础和python(二)

    一,五层协议 应用层    端口 传输层   tcp\udp 网络层   ipv4\6 数据链路层  ethernet 物理层    mac 二:什么是变量? 变量:核心在于变和量儿字,变->变 ...

  7. Javascript异步编程的4种方法(阮一峰)

    转载: http://www.ruanyifeng.com/blog/2012/12/asynchronous%EF%BC%BFjavascript.html 你可能知道,Javascript语言的执 ...

  8. 【LeetCode】2. Add Two Numbers 两数相加

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  9. 初级安全入门——XSS注入的原理与利用

    XSS的简单介绍 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets,CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者 ...

  10. 【eclipse jar包】在编写java代码时,为方便编程,常常会引用别人已经实现的方法,通常会封装成jar包,我们在编写时,只需引入到Eclipse中即可。

    Eclipse中导入外部jar包 在编写java代码时,为方便编程,常常会引用别人已经实现的方法,通常会封装成jar包,我们在编写时,只需引入到Eclipse中即可. 工具/原料 Eclipse 需要 ...