原题地址: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的更多相关文章

  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. Java 内部类.md

    Java 内部类 学习自 <Java编程思想> Overview 什么是内部类? Thinking In Java 中如此定义: 将一个类的定义放在里另一个类的定义的内部,这就是内部类. ...

  2. android studio 查看大纲

    就是 structure 面板 快捷键 Alt+7 === android studio 查看方法说明 点击菜单“View”-“Quick Documentation" 建议直接查看源代码文 ...

  3. Java内存泄露分析和解决方案及Windows自带查看工具

    Java内存泄漏是每个Java程序员都会遇到的问题,程序在本地运行一切正常,可是布署到远端就会出现内存无限制的增长,最后系统瘫痪,那么如何最快最好的检测程序的稳定性,防止系统崩盘,作者用自已的亲身经历 ...

  4. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  5. poll() can't detect event when socket is closed locally?

    from https://stackoverflow.com/questions/5039608/poll-cant-detect-event-when-socket-is-closed-locall ...

  6. 电子书下载:Delphi XE 5 移动开发入门手册(完整版)

    更多电子书请到: http://maxwoods.400gb.com 下载:Delphi XE5移动开发入门手册(完整版)

  7. Visual Studio 2013 sqlce 配置(转)

    Visual Studio 2013 把內建 SQL CE 的管理工具拿掉了 下载SQL Server Compact Toolbox by ErikEJ并安装 打开VS2013,新建一工程,在“视图 ...

  8. ECShop 调用自定义广告

    原文地址:http://www.ecshoptemplate.com/article-1348.html ECShop中关于广告的调用方法,网上有很多,现在要介绍的不同于其他,根据实际情况选择使用,以 ...

  9. Reflector_8.3.0.93_安装文件及破解工具

    Reflector_8.3.0.93_安装文件及破解工具 下载地址:http://pan.baidu.com/s/1jGwsYYM   约 8.9MB

  10. ASP.NET MVC:4 Ways To Prevent Duplicate Form Submission(转载)

    原文地址:http://technoesis.net/prevent-double-form-submission/. Double form submission in a multi-user w ...