[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. ...
随机推荐
- 慎重使用volatile关键字
volatile关键字相信了解Java多线程的读者都很清楚它的作用.volatile关键字用于声明简单类型变量,如int.float.boolean等数据类型.如果这些简单数据类型声明为volatil ...
- Android-Window(一)——初识Window
Android-Window(一)--初识Window 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/detail ...
- java 中的同步机制
对于有些场景,需要a.b线程按照顺序去执行,因为b线程要依赖a线程对某共享资源或 状态处理后,对于这种情况可以使用 private CountDownLatch connectedSignal = n ...
- 4951: [Wf2017]Money for Nothing 决策单调性 分治
Bzoj4951:决策单调性 分治 国际惯例题面:一句话题面:供应商出货日期为Ei,售价为Pi:用户收购截止日期为Si,收购价格为Gi.我们要求max((Si-Ej)*(Gi-Pj)).显然如果我们把 ...
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
- python使用递归实现一个分形图形
代码如下: import turtle def main(): t = turtle.Turtle() t.hideturtle() t.speed(10) level = 12 fract(t,-8 ...
- JSONPATH使用方法
如下的json: { "store": { "book": [ { "category": "reference", & ...
- C/C++中结构体(struct)
c++ 里面struct可以new,另外: C++中,struct关键字与Class关键字基本是一样的,但是,有两点不同 1 struct定义的数据类型里面所有成员默认级别都是共有的,而class里面 ...
- MQ:Introducing Advanced Messaging
原文地址:http://www.yourenterprisearchitect.com/2011/11/introducing-advanced-messaging.html. Introducing ...
- python测试开发django-37.外键(ForeignKey)查询
前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关 ...