题目 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. URL优化的几个处理方法

    原文链接:http://www.51chinashop.com/shopinfo/dsjs/2014-11-23/100.html 在一个网站中,一个页面对应了多个URL,必然会分散这个页面的权重.因 ...

  2. [UE4]区分敌我

    第一方法是:使用Actor对象的Tag: 只要继承自Actor的对象,都会有Tags这个属性,这是一个数组,可以添加多个Tag. 在蓝图中可以使用“get Components by Tag”获得某个 ...

  3. Could not determine own NN ID in namespace 'mycluster'

    执行hdfs namenode -bootstrapStandby的时候报错如下 19/03/24 18:00:48 ERROR namenode.NameNode: Failed to start ...

  4. ARCGIS 出图显示not map metafile into memory.Not enough memory

    有许多的原因,比如系统有问题,磁盘空间不够,或虚拟内存设置不对等.再有就是输出地图时分辨率的设置是否太大等.    not enough memory     这个问题是ESRI的一个所谓的“臭名昭著 ...

  5. win7右键菜单调整顺序

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ Discardable\PostSetup\ShellNew ...

  6. mac一些设置

    Mac自带了的JDK6,安装在目录:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/下. JDK8则需要自己到Oracle官网下载安装对应的版本. ...

  7. DNS(bind)服务器安装和配置

    一.前言 DNS 域名系统(英文:Domain Name System,缩写:DNS)是因特网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.DNS使用TCP ...

  8. Could not resolve all files for configuration;Andriod在build.gradle添加compile files()报错

    在build.gradle中添加个 compile files('libs/alipaySdk-20170922.jar') 就一直报这个错误 Error:Could not resolve all ...

  9. live2d添加网页看板娘

    最近逛博客,发现好多博主都加了网页看板娘,简直可爱到血槽空了,赶紧学习模仿改造了一下~ 给博客园的博客也添加了看板娘!!就在右边~喜欢的请打赏我~ 不过因为移植过来比较复杂,就不出教程啦~记得多来调教 ...

  10. 获取cookie后,使用cookie进行接下来的自动化操作

    System.setProperty("javax.net.ssl.trustStore", certPath); public void uploadComponent() th ...