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. TabLayout实现顶部导航(一)

    代码地址如下:http://www.demodashi.com/demo/14552.html 前言 顶部导航栏,是我们在开发中比较常见的一种显示布局,它的实现可以有多种方式,那么今天我们就来讲讲 T ...

  2. HTML5动感圆圈

    概述 利用HTML5 Canvas实现的炫酷动感的圆圈动画. 详细 代码下载:http://www.demodashi.com/demo/10419.html 一.准备工作 本demo使用HTML5进 ...

  3. ocat

    <!DOCTYPE html> <html lang="zh-CN" > <head><meta http-equiv="Con ...

  4. 【微信小程序】 引用公共js里的方法

    一个小程序页面由四个文件组成,一个小程序页面的四个文件具有相同路径与文件名,由此我们可知一个小程序页面对应着一个跟页面同名的js文件.可是当有些公共方法,我们想抽离出来成为一个独立公共的js文件.我们 ...

  5. mysql中的慢查询日志

    首先我们看一下关于mysql中的日志,主要包含:错误日志.查询日志.慢查询日志.事务日志.二进制日志: 日志是mysql数据库的重要组成部分.日志文件中记录着mysql数据库运行期间发生的变化:也就是 ...

  6. java项目中classpath路径到底指的是哪里?

    本文转自:http://blog.csdn.net/javaloveiphone/article/details/51994268 1.src不是classpath, WEB-INF/classes和 ...

  7. java第四章编程题(初学篇)

    代码: /* test.java */ package test; public class test { public static void main(String args[] ) { CPU ...

  8. Windows命令-route

    Windows route命令 添加一条永久网关:route add 0.0.0.0 mask 0.0.0.0 192.168.2.1 -p例如: route -p add 192.168.0.0 m ...

  9. 数据源与JNDI资源实现JSP数据库连接池实例

    名词解释:JNDI的全称是java命名与目录接口(Java Naming and Directory Interface),是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通 ...

  10. linux的cat命令

    1 描述 cat 的全称 concatenate files and print on the standard output cat命令事Linux下的一个文本输出命令. 用于链接文件并打印到标准输 ...