题目 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. VLAN IEEE802.1Q

    一. VLAN产生原因-广播风暴 传统的局域网使用的是HUB,HUB只有一根总线,一根总线就是一个冲突域.所以传统的局域网是一个扁平的网络,一个局域网属于同一个冲突域.任何一台主机发出的报文都会被同一 ...

  2. echarts饼图配置

    js引用和配置div <div id="container" style="height: 100%"></div> <scrip ...

  3. CRM 更新解决方案之注意事项

    一般需要开发新功能时,企业或者软件公司往往会先从生产环境克隆出一台测试用系统. 开发人员会在测试系统中对功能进行开发或者测试. 这时当新功能开发和测试完成之后,需要将新的解决方案导入生产环境. 导入时 ...

  4. VMware安装CentOS7踩坑

      1.VMware安装Centos7加载界面不显示,但有快照 单击“开始”,运行中输入cmd. 然后输入命令 netsh winsock reset. 重启计算机. 2.nat模式网络问题     ...

  5. DB通用类:Sqlite通用类库

    Sqlite通用类库 using System; using System.Collections; using System.Collections.Generic; using System.IO ...

  6. Spqrk笔记

    LSM:Least square method 最小二乘法 ALS:Alternating Least Squares 交替最小二乘法 http://blog.csdn.net/dreamer2020 ...

  7. ubuntu 16.04 配置静态ip 后默认的网卡eno1变成eth0了不能联网的问题解决

    我这次是在真实机器上面安装的ubuntu16.04 在配置了静态ip后不懂什么原因默认的eno1网卡变回了eth0网卡之后就不能上网, 同一个网段的其他集群节点也不能ping 通 因为ubuntu16 ...

  8. JS中 == ,===, !=, !==的区别

    一个等号是赋值操作,==先转换类型再比较,===先判断类型,如果不是同一类型直接为false. === 判断规则 如果类型不同,就[不相等]  如果两个都是数值,并且是同一个值,那么[相等]:(!例外 ...

  9. 【Tomcat】Tomcat安装及Eclipse配置教程

    ==================================================================================================== ...

  10. IntelliJ Idea设置Could not autowire. No beans of 'xxx' type found

    1.问题描述 在Idea的spring工程里,经常会遇到Could not autowire. No beans of ‘xxxx’ type found的错误提示.但程序的编译和运行都是没有问题的, ...