原题地址: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)]。

代码:

  1. # Definition for a point
  2. # class Point:
  3. # def __init__(self, a=0, b=0):
  4. # self.x = a
  5. # self.y = b
  6.  
  7. class Solution:
  8. # @param points, a list of Points
  9. # @return an integer
  10. def maxPoints(self, points):
  11. length = len(points)
  12. if length < 3: return length
  13. res = -1
  14. for i in range(length):
  15. slope = {'inf': 0}
  16. samePointsNum = 1
  17. for j in range(length):
  18. if i == j:
  19. continue
  20. elif points[i].x == points[j].x and points[i].y != points[j].y:
  21. slope['inf'] += 1
  22. elif points[i].x != points[j].x:
  23. k = 1.0 * (points[i].y - points[j].y) / (points[i].x - points[j].x)
  24. if k not in slope:
  25. slope[k] = 1
  26. else:
  27. slope[k] += 1
  28. else:
  29. samePointsNum += 1
  30. res = max(res, max(slope.values()) + samePointsNum)
  31. return res

[leetcode]Max Points on a Line @ Python的更多相关文章

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

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

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

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

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

  6. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

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

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

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

随机推荐

  1. 慎重使用volatile关键字

    volatile关键字相信了解Java多线程的读者都很清楚它的作用.volatile关键字用于声明简单类型变量,如int.float.boolean等数据类型.如果这些简单数据类型声明为volatil ...

  2. Android-Window(一)——初识Window

    Android-Window(一)--初识Window 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/detail ...

  3. java 中的同步机制

    对于有些场景,需要a.b线程按照顺序去执行,因为b线程要依赖a线程对某共享资源或 状态处理后,对于这种情况可以使用 private CountDownLatch connectedSignal = n ...

  4. 4951: [Wf2017]Money for Nothing 决策单调性 分治

    Bzoj4951:决策单调性 分治 国际惯例题面:一句话题面:供应商出货日期为Ei,售价为Pi:用户收购截止日期为Si,收购价格为Gi.我们要求max((Si-Ej)*(Gi-Pj)).显然如果我们把 ...

  5. Codeforces Round #258 (Div. 2) . Sort the Array 贪心

    B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...

  6. python使用递归实现一个分形图形

    代码如下: import turtle def main(): t = turtle.Turtle() t.hideturtle() t.speed(10) level = 12 fract(t,-8 ...

  7. JSONPATH使用方法

    如下的json: { "store": { "book": [ { "category": "reference", & ...

  8. C/C++中结构体(struct)

    c++ 里面struct可以new,另外: C++中,struct关键字与Class关键字基本是一样的,但是,有两点不同 1 struct定义的数据类型里面所有成员默认级别都是共有的,而class里面 ...

  9. MQ:Introducing Advanced Messaging

    原文地址:http://www.yourenterprisearchitect.com/2011/11/introducing-advanced-messaging.html. Introducing ...

  10. python测试开发django-37.外键(ForeignKey)查询

    前言 前面在admin后台页面通过设置外键,可以选择下拉框的选项,本篇主要讲解关于外键(ForeignKey)的查询 models设计 在上一篇的基础上新增一个BankName表,Card表通过外键关 ...