[LeetCode]题解(python):149-Max Points on a Line
题目来源:
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的更多相关文章
- 【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 ...
- [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】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- [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. ...
- 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. ...
- 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. ...
- 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. ...
- 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 ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- 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. ...
随机推荐
- .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending
.NET 3.5(5) - LINQ查询操作符之Select.Where.OrderBy.OrderByDescending 介绍 ·Select - Select选择:延迟 ·Where ...
- EC读书笔记系列之19:条款49、50、51、52
条款49 了解new-handler的行为 记住: ★set_new_handler允许客户指定一个函数,在内存分配无法获得满足时被调用 ★Nothrow new是一个颇为局限的工具,∵其只适用于内存 ...
- zend guard loader
1 .是zendoptimizer的前身, 在php 5.3 (含)之前使用更新到6 ,5.4 之后不再使用.是代码优化的一种,7中opcache 类似功效. 2 .php版本的变量 phpversi ...
- PHP中的数组方法及访问方法总结
一.数组操作的基本函数 数组的键名和值 array_values($arr);获得数组的值 array_keys($arr);获得数组的键名 array_flip($arr);数组中的值与键名互换(如 ...
- vb combobox 用法问题总结
问题一 combobox 通过type类型,如下代码,通过选取name名称(改变combobox的名称)得到 其Id Type User id As Integer userName As Strin ...
- Http网络请求
前提说明: 1.Str.Empty 其实就是 string.Empty 2.@object.IsNull() 的IsNull() 是判断“是否等于null”的扩展方法,等同于 @object==nu ...
- eclipse 代码补全
代码补全 window-> properties -> Java ->Editor ->content Assist -> Auto activation trigger ...
- 40. Testing Prev Part IV. Spring Boot features
40. Testing Spring Boot provides a number of utilities and annotations to help when testing your app ...
- <!DOCTYPE html>
html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5.
- MVC中使用AuthorizeAttribute做身份验证操作【转】
http://blog.csdn.net/try530/article/details/7782704 代码顺序为:OnAuthorization-->AuthorizeCore-->Ha ...