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 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 解题报告的更多相关文章
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- [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 ...
- [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. ...
- [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 ...
- 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 ...
- 【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 ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- 【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 ...
- [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. ...
随机推荐
- 文本框input:text
文本框 CreateTime--2017年4月24日10:40:40 Author:Marydon 一.文本框 (一)标签 <input type="text"/> ...
- TOMCAT问题总结
迁移时间--2017年7月9日14:58:12Author:Marydon CreateTime--2016年12月25日21:55:09Author:MarydonTomcat问题总结问题一 A ...
- url请求返回结果测试工具(CURL)
官网:http://curl.haxx.se/download.html 具体用法用时百度 或 到时再补充
- Linux下TCP最大连接数受限问题
一. 文件数限制修改1.用户级别查看Linux系统用户最大打开文件限制:# ulimit -n1024 (1) vi /etc/security/limits.confmysql soft nofil ...
- java随机函数使用方法Random
import java.util.Random; public class RandomNumber{ public static void main(String[] args) { // 使用ja ...
- Java虚拟机学习 - 对象引用强度 ( 8 )
无论是通过计数算法判断对象的引用数量,还是通过根搜索算法判断对象引用链是否可达,判定对象是否存活都与“引用”相关. 引用主要分为 :强引用(Strong Reference).软引用(Soft Ref ...
- python 小游戏之摇骰子猜大小
最近学习Python的随机数,逻辑判断,循环的用法,就想找一些练习题,比如小游戏猜大小,程序思路如下: 开发环境:python2.7 , 附上源代码如下: 摇骰子的函数,这个函数其实并不需要传任何参数 ...
- Python asin() 函数
描述 asin() 返回x的反正弦弧度值. 语法 以下是 asin() 方法的语法: import math math.asin(x) 注意:asin()是不能直接访问的,需要导入 math 模块,然 ...
- oracle字符串处理函数--待整理
http://www.cnblogs.com/xd502djj/archive/2010/08/11/1797577.html http://blog.csdn.net/qq373591361/art ...
- IndexOf、LastIndexOf、Substring的用法及C# foreach 中获取索引index的方法
String.IndexOf String.IndexOf 方法 (Char, Int32, Int32)报告指定字符在此实例中的第一个匹配项的索引.搜索从指定字符位置开始,并检查指定数量的字符位置 ...