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 的网格上玩井字棋. 井字棋游戏的规则如下: ...
随机推荐
- [UE4]UI动画
- Linux IO多路复用 select/poll/epoll
Select -- synchronius I/O multiplexing select, FS_SET,FD_CLR,FD_ISSET,FD_ZERO #include <sys/time. ...
- Mysql 64位解压版的安装
先下载解压版的mysql 下载地址 https://dev.mysql.com/downloads/file/?id=474496 解压 进到里面新建这个文件夹和文件 打开my.ini文件(用文本编辑 ...
- java对redis的基本操作(初识)
一.server端安装 1.下载 https://github.com/MSOpenTech/redis 可看到当前可下载版本:redis2.6
- Linux常用操作分享
Java开发经常遇到的linux相关操作 1.常用的上传下载(Xshell5) 1).get 从远程服务器上下载一个文件存放到本地,如下: 先通过lcd切换到本地那个目录下,然后通过get file ...
- python-requests数据驱动延伸
在 python-requests模块的讲解和应用 基础上进行数据驱动的延伸 task_01_requests.py #-*- coding:utf-8 -*- #task_01_requests.p ...
- 数据库设计和ER模型-------之关系模型的基本概念(第二章)
关系模型的基本术语 定义:用二维表格来表示实体集,用关键码表示实体之间联系的数据模型称为关系模型 有时也习惯称呼关系为表或表格,元组为行(Row),属性为列.关系中属性个数称为“元数”,元组个数称为“ ...
- linux中ps命令
ps的参数 -C的使用 [root@centos7 ~]# ps -C nginx -o user,pid,comm USER PID COMMAND root 2697 nginx nginx 26 ...
- web前端基本开发手册
--------------------------------- 一.概况 1.1 WEB 标准 二.实现WEB标准 2.1 XHTML.CSS介绍 2.2 XHTML书写规范 2.2.1 X ...
- Ruby学习笔记7: 添加身份验证(adding Authentication)
我们已经完成了Category & Product页面内容的增删改查,再加入一个身份验证即可成为一个较完整的Rails App了.本文就来完成这个任务. We now need to give ...