leetcode ex3 找出穿过最多点的直线 Max Points on a Line
题目 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的更多相关文章
- 【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 ...
- 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 ...
- LeetCode 5071. 找出所有行中最小公共元素(Java)
题目:5071. 找出所有行中最小公共元素 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1 ...
- Java实现 LeetCode 719 找出第 k 小的距离对(二分搜索法+二分猜数字)
719. 找出第 k 小的距离对 给定一个整数数组,返回所有数对之间的第 k 个最小距离.一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值. 示例 1: 输入: nums = [1,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. ...
- 【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. ...
- 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 ...
- 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 ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
随机推荐
- IP地址基础
第一台计算机的名字 1946年2月14日,世界上第一台电脑ENIAC在美国宾夕法尼亚大学诞生,名叫ENIAC(爱尼阿克). 第一个网络的名字: arpanet 计算机网络定义: 物理位置不同.功能 ...
- vultr上 windows使用pptp拨号来实现冗余双网关的解决方案
rasdial是拨号程序,pptpvpn是网卡拨号名称,后面跟的是帐号和密码.pptpvpn见下图:就是提前创建好一个PPTP的拨号连接 上面是启动时候的计划任务,那么万一拨号中断,要继续重拨还需要做 ...
- vim查找关键字的好方法
当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢? 在vi里可没有菜单-〉查找 不过没关系,你在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然 ...
- CTF PHP文件包含--session
PHP文件包含 Session 首先了解一下PHP文件包含漏洞----包含session 利用条件:session文件路径已知,且其中内容部分可控. 姿势: php的session文件的保存路径可以在 ...
- CRM 2016 一个IFrame_A 刷新另一个 IFrame_B
思路是 : 1 创建一个字段“new_xxx”. 2 注册字段 OnChange 事件. 3 OnChange 事件 刷新 IFrame_B子页面. CRM父页面JS: /// <summary ...
- DELPHI中自定义消息的发送和接收
DELPHI中的消息处理机制 Delphi是Borland公司提供的一种全新的WINDOWS编程开发工具.由于它采用了具有弹性的和可重用的面向对象Pascal(object-orientedpasca ...
- dataset to list
http://www.c-sharpcorner.com/UploadFile/ee01e6/different-way-to-convert-datatable-to-list/ http://ww ...
- c#数组去重
第一种: string[] stringArray = { "aaa", "bbb", "aaa", "ccc", &q ...
- 基于Boost的同步TCP通信
1:客户端 #include <iostream> #include <boost/asio.hpp> using namespace boost; using namespa ...
- Android蓝牙BLE开发,扫描、连接、发送和读取信息;
1.BLE开发权限 Android蓝牙BLE开发须打开蓝牙权限和6.0位置权限: <uses-permission android:name="android.permission.B ...