[leetcode]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/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.
解题思路:找到平面上在一条直线上最多的点。点在同一条直线上意味着这些点的斜率是一样的,那么可以考虑使用哈希表来解决,{斜率:[点1,点2]}这样的映射关系。这里有几个需要考虑的要点:1,有可能是斜率无穷大。2,有可能有相同的点,比如[(1,2),(1,2)]。
代码:
# 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):
length = len(points)
if length < 3: return length
res = -1
for i in range(length):
slope = {'inf': 0}
samePointsNum = 1
for j in range(length):
if i == j:
continue
elif points[i].x == points[j].x and points[i].y != points[j].y:
slope['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 not in slope:
slope[k] = 1
else:
slope[k] += 1
else:
samePointsNum += 1
res = max(res, max(slope.values()) + samePointsNum)
return res
[leetcode]Max Points on a Line @ Python的更多相关文章
- 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] 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 题解
题意 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...
- 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】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 OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- 【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 ...
- [LintCode] 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多点共线
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
随机推荐
- commonjs,amd,cmd
在某些库中,经常会看到函数最前面有一个分号.其实是为了防止自动化工具拼接js时,如果前面的js文件的结尾处忘了加分号,拼接出来的代码容易挂,加分号这种行为属于防御式编程. 一个模块就是实现特定功能的文 ...
- zk节点扩充
zk节点扩充,从3个节点扩充为7个节点,需要先安装4个节点,在将4个节点的配置进行修改,然后在修改 原有的3个节点.至此完成对zk的扩充实现,在此做个记录. zk节点的顺序,与对应zk与所在序列保持一 ...
- Git和Gitlab
参考 http://www.cnblogs.com/clsn/p/7929958.html#auto_id_16https://backlog.com/git-tutorial/cn/intro/in ...
- Java Web c3p0 pool池泄漏优化与日志分析
问题跟踪: 近期在整合SSH(spring.springmvc.hibernate)项目,提供给第三方服务.每当调用内存池达到上限之后,外界调用服务直接失败,提示[cannot open connec ...
- AutoMapper实际项目运用
AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 配置AutoMapper映射规则 AutoMapper是基于约定的,因此在实用映射之前 ...
- RC4加密算法的原理及实现
RC4于1987年提出,和DES算法一样.是一种对称加密算法,也就是说使用的密钥为单钥(或称为私钥). 但不同于DES的是.RC4不是对明文进行分组处理,而是字节流的方式依次加密明文中的每个字节.解密 ...
- What is OpenOCD?
About OpenOCD was created by Dominic Rath as part of a 2005 diploma thesis written at the University ...
- Context Switching on the Cortex-M3
http://coactionos.com/embedded%20design%20tips/2013/10/09/Tips-Context-Switching-on-the-Cortex-M3/ T ...
- [Git]git教程
摘要 目前公司项目逐渐都要迁移到git上,使用git进行版本控制及源代码管理. git学习资料 一个小时学会Git 权威Git书籍ProGit(中文版) git官网:http://git-scm.co ...
- 自己动手实现一个MVVM库
我们知道的,常见的数据绑定的实现方法 1.数据劫持(vue):通过Object.defineProperty() 去劫持数据每个属性对应的getter和setter2.脏值检测(angular):通过 ...