题目 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. 安装部署Jenkins服务

    1.导入安装包 2.安装安装包 (1).安装JDK和maven 创建jdk存储目录 # mkdir /usr/jdk64/ 解压jdk到存储目录 # tar -zxvf jdk-8u191-linux ...

  2. JavaScript、CSS样式收集

    JS集: //给from一个名字然后在JavaScript的地方就可以用form的名字来调用form表单里input元素的value属性可以得到值 var val=form_name.input_na ...

  3. 几个常用的SQL 时间函数

    --当月第一天declare @startFirstDate datetimeset @startFirstDate=dateadd(dd,datediff(dd,0,getdate()),-day( ...

  4. POJ K-th Number

    [题解] 数据结构采用线段树.通过将数组的每一段归并排序来建树.将数组排序来实现离散化. 时间复杂度分析:建树的过程就是归并排序,其时间复杂度为O(nlog(n)).查询时:二分查找第k小元素的复杂度 ...

  5. JS一个算法题

    题目:实现超出整数存储范围的两个大整数想加function(a,b).注意:参数a和b以及函数返回值都是字符串. 目的:考算法, 基本逻辑. 我实现的基本思路是: ①两个数字字符串长度补成一样,用字符 ...

  6. mono_image_open_from_data_with_name原型

    mono4.5 https://github.com/Unity-Technologies/mono 查看mono源码: //PATH: /mono/metadata/image.c MonoImag ...

  7. KVM总结-KVM性能优化之磁盘IO优化

    前面讲了KVM CPU(http://blog.csdn.net/dylloveyou/article/details/71169463).内存(http://blog.csdn.net/dyllov ...

  8. 马哥Linux base学习笔记

     介绍课程: 中级: 初级:系统基础 中级:系统管理.服务安全及服务管理.shell脚本 高级: MySQL数据库: Cache & storgae 集群: Cluster   lb: 4la ...

  9. 内存大小设置 Java heap space错误

    1. 问题描述 当从数据库中查询大量的数据,每个模板取出来几百万条数据,或者是频繁的刷新项目.模板时就会占用Java虚拟机JVM的大量内存,超过内存就会出现报java.lang.OutOfMemory ...

  10. Struts2学习:Action使用@Autowired注入为null的解决方案

    1.pom.xml引入struts2-spring-plugin <dependency> <groupId>org.apache.struts</groupId> ...