原题地址: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. php版本CKEditor 4和CKFinder安装及配置

    下载并解压CKEditor 4和CKFinder CKEditor 4下载地址:https://ckeditor.com/cke4/builder,选择自定义的版本,记得加上中文语言包 CKFinde ...

  2. UEditor 的使用

    UEditor 的使用 一.UEditor 的使用 官方网站:http://ueditor.baidu.com/website/ 下载地址:http://ueditor.baidu.com/websi ...

  3. 解决浏览器抛出乱码,(HTML、PHP等的乱码问题)

    在Windows上编写html或php代码的时候,本地编辑器设置的文件编码格式是utf-8保存,但是浏览器打开页面的时候经常出现乱码,而且浏览器自动检测到的页面编码为GBK格式,这时候我就开始纳闷了? ...

  4. 最新的裸机联想笔记本装win7系统/SSD(固态硬盘)上安装win7系统/联想K4450A i7装win7系统

    老师让我帮他装个操作系统,由于是新电脑,并且老师的电脑上另安有固态硬盘,老师要我把系统安装在固态硬盘上,BIOS是2014年7月份的,所以BIOS设置项可能会有所变化. 下面是遇到的一些问题,及解决方 ...

  5. [Java]jdbc[转]

    >>http://www.cnblogs.com/xiohao/p/3507483.html >>http://www.cnblogs.com/hongten/archive/ ...

  6. 喵哈哈村的魔法考试 Round #5 (Div.2) 题解

    老规矩 有问题直接联系我:475517977@qq.com A 直接暴力的for一遍,统计连续的有多少个就好了.模拟题. #include<bits/stdc++.h> using nam ...

  7. CentOS 7解压安装PHP7.1.21

    下载php yum install -y wget wget http://cn2.php.net/distributions/php-7.1.21.tar.gz 解压 tar -zxvf php-7 ...

  8. Serial Wire Viewer (SWV)

    Being able to display values for counters, sensors and other debugging information is an important p ...

  9. [Go] 通过 17 个简短代码片段,切底弄懂 channel 基础

    关于管道 Channel Channel 用来同步并发执行的函数并提供它们某种传值交流的机制. Channel 的一些特性:通过 channel 传递的元素类型.容器(或缓冲区)和 传递的方向由“&l ...

  10. ERROR 2002 (HY000): Can&#39;t connect to local MySQL server through socket

    在安装好了MySQL之后,使用了新的配置文件后.MySQL服务器能够成功启动,但在登陆的时候出现了ERROR 2002 (HY000): Can't connect to local MySQL se ...