题目来源:

  https://leetcode.com/problems/max-points-on-a-line/


题意分析:

  在一个2D的板上面有很多个点,判断最多有多少个点在同一条直线上。


题目思路:

  这里我们可以用斜率来记录两条边是否在同一条直线。如果考虑再细一点,由于double有精度的问题,斜率最后用分数来表示。


代码(python):

# Definition for a point.
# class Point(object):
# def __init__(self, a=0, b=0):
# self.x = a
# self.y = b class Solution(object):
def maxPoints(self, points):
"""
:type points: List[Point]
:rtype: int
"""
size = len(points)
if size < 3:
return size
ans = 0
for i in range(size):
d = {'inf':0}
samePoint = 1
for j in range(size):
if i == j:
continue
elif points[i].x == points[j].x and points[i].y != points[j].y:
d['inf'] += 1
elif points[i].x != points[j].x:
k = 1.0 * (points[i].y - points[j].y) / (points[i].x - points[j].x)
if k in d:
d[k] += 1
else:
d[k] = 1
else:
samePoint += 1
ans = max(ans,max(d.values()) + samePoint)
return ans

[LeetCode]题解(python):149-Max Points on a Line的更多相关文章

  1. 【LeetCode】149. 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 ...

  2. [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. ...

  3. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

  4. [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. ...

  5. Java for 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 149. Max Points on a Line --------- java

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  7. 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. ...

  8. 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 li ...

  9. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  10. 149. 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. ...

随机推荐

  1. 7.19 SQL——函数

    select * from student select * from score select * from teacher select * from course select * from c ...

  2. MongoDB 操作

    通过CMD命令操作数据 确保MongoDB的服务已经正常安装,记下安装目录(D:\MongoDB\Service) 然后打开cmd 先转到D盘 cd D: 然后转到服务的安装目录下 cd D:\Mon ...

  3. JavaScript 对象扩展代码

    JavaScript 扩展代码 更具需要写的几个扩展. 扩展核心自执行函数 Object.extend /** * 对象扩展体 参数是 {属性|方法:属性值|方法体} * 只执行实现 * * 实例对基 ...

  4. php 数组去除空值

    /** * 方法库-数组去除空值 * @param string $num 数值 * @return string */ public function array_remove_empty(& ...

  5. *循环-01. 求整数段和【help】

    /* * Main.c * 循环-01. 求整数段和 * Created on: 2014年6月18日 * Author: Boomkeeper ***测试木有通过**** */ #include & ...

  6. Android06-Fragment碎片

    ¨Fragment简介 ¨Fragment生命周期 ¨动态加载碎片Fragment Manager的使用   1.Fragment表示Activity中的一种行为或者一部分用户界面. 可以将Fragm ...

  7. USB2.0的基本学习

    SB2.0是在1.0的基础上于2000年提出来的,在1.0的基础上曾加了480Mbps的数据传输率.USB2.0具有以下的优点: 1.每个USB系统中有一个主机,通过级联的方式连接多个外部设备,最多可 ...

  8. Linux中service命令和/etc/init.d/的关系

    Linux中service命令和/etc/init.d/的关系   service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...

  9. 使用kd-tree加速k-means

    0.目录 前置知识 思路介绍 详述 1 确定h的中心点 2 算法步骤 java实现 1.前置知识 本文内容基于<Accelerating exact k-means algorithms wit ...

  10. sqlserver数据库 提纲

    sqlserver数据库一.体系结构文件--服务--界面 文件---.maf .ldf .ndf服务--MSSqlserver,别名(实例名)界面--宋庆龄serve如何windouws身份验证,如何 ...