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.

SOLUTION 1:

全部的点扫一次,然后计算每一个点与其它点之间的斜率。

创建一个MAP, KEY-VALUE是 斜率:线上的点的数目。

另外,注意重合的点,每次都要累加到每一条线上。

注意:

1. k = 0 + (double)(points[i].y - points[j].y)/(double)(points[i].x - points[j].x);

使用这个公式来计算的原因是 (double)(points[i].y - points[j].y)/(double)(points[i].x - points[j].x) 有可能计算出0和-0

0+(-0)后,就会都变成0.

2. 用Dup来计算重合的点。

3. 如果某点开始所有的点都在一起,则至少Max = Math.max(max, duplicate)。

4. 注意每次换一个点计算时,map要重建。因为即使K相同,只代表线是平等,不代表会是同一条线。

 public class Solution {
public int maxPoints(Point[] points) {
int max = ; if (points == null) {
return ;
} int len = points.length; for (int i = ; i < len; i++) {
// Create a map to recode all the numbers of elements of every K.
HashMap<Double, Integer> map = new HashMap<Double, Integer>(); // ItSelf.
int dup = ; for (int j = i; j < len; j++) {
// the same point.
if (points[i].x == points[j].x && points[i].y == points[j].y) {
dup++;
continue;
} double k = Double.MAX_VALUE;
if (points[i].x != points[j].x) {
k = + (double)(points[i].y - points[j].y)/(double)(points[i].x - points[j].x);
} if (map.containsKey(k)) {
map.put(k, map.get(k) + );
} else {
map.put(k, );
}
} max = Math.max(max, dup);
for (int n: map.values()) {
max = Math.max(max, n + dup);
}
} return max;
}
}

主页君的GitHub:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/hash/MaxPoints.java

参考答案:

http://www.ninechapter.com/solutions/max-points-on-a-line/

LeetCode: Max Points on a Line 解题报告的更多相关文章

  1. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

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

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

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

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

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

  7. [LeetCode OJ] Max Points on a Line

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

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

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

随机推荐

  1. 【BIRT】在页面上展示xxxx年xx月xx日

    我们在做报表开发的时候经常会遇到一个问题,就是需要在报表上展示”xxxx年xx月xx日”这种日期,例如:需要在报表展示日期如下图: 我们现在数据库存储的日期是:20171231 那么我们如何转化为 这 ...

  2. VB.NET服务器端令客户端下载PDF文件

    后台JS调用另一个控件,通过SESSION传递sDocumentPath 控件后台代码如下     Response.Clear() '如果不清,则有可能将页面源码作为文件内容的一部分传递给用户    ...

  3. LoadRunner内部结构

    转载自:http://blog.sina.com.cn/s/blog_6da75b980100n2nv.html   英文版地址: http://www.rickyzhu.com/21_princip ...

  4. 升级 asp.net core 1.1 到 2.0 preview

    Upgrading to .NET Core 2.0 Preview 1 更新 依赖的类库 改为 标准库 2 web app  更改 csproj 文件---升级版本 <PropertyGrou ...

  5. 重要:VC DLL编程

    VC DLL编程 静态链接:每个应用程序使用函数库,必须拥有一份库的备份.多个应用程序运行时,内存中就有多份函数库代码的备份. 动态连接库:多个应用程序可以共享一份函数库的备份. DLL的调用方式:即 ...

  6. iOS支付宝支付集成

    概述 iOS支付宝支付集成 详细 代码下载:http://www.demodashi.com/demo/10729.html 支付宝和微信都是业界的老大哥,相信大家都有所觉得文档.SDK都是各种坑吧( ...

  7. 在linux下安装并使用websocket

    前言 首先,对websocket要有一个简要的了解与认识 websocket是HTML5开始提供的一种浏览器与服务器进行全双工通讯的网络技术,属于应用层协议. 它基于TCP传输协议,并复用HTTP的握 ...

  8. linux下修改文件权限

    加入-R 参数,就可以将读写权限传递给子文件夹例如chmod -R 777 /home/mypackage那么mypackage 文件夹和它下面的所有子文件夹的属性都变成了777777是读.写.执行权 ...

  9. java 安装配置时出现的问题

    Error: could not open `C:\Program Files\Java\jre6\lib\i386\jvm.cfg') jdkerror  前些日子装了个jdk7试了试,后来做项目需 ...

  10. HDUOJ---The Moving Points

    The Moving Points Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...