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. ...
随机推荐
- SDUT 1157-小鼠迷宫问题(BFS&DFS)
小鼠迷宫问题 nid=24#time" title="C.C++.go.haskell.lua.pascal Time Limit1500ms Memory Limit 65536 ...
- 【SPSS】软件介绍
SPSS软件是美国斯坦福大学三位学生1968年研制开发的统计软件,SPSS是Statistical Package for Social Science(社会科学软件统计包)的缩写,2000年SPSS ...
- Asp.net Mvc (Filter及其执行顺序)
应用于Action的Filter 在Asp.netMvc中当你有以下及类似以下需求时你可以使用Filter功能判断登录与否或用户权限,决策输出缓存,防盗链,防蜘蛛,本地化设置,实现动态Actionfi ...
- C#:定义窗口快捷键
事情的关键是要设置Form的KeyPreview属性,然后再在KeyDown事件中检查按键. public class TEST : Form { public TEST() { Initialize ...
- TaskController
package main.java.com.zte.controller.system; import java.io.PrintWriter; import java.util.ArrayList; ...
- 利用Aspose.BarCode生成条码
生成条码有很多控件,大部分好的控件,做出来的条码精确清晰,但是往往该部分的控件费用都很高,这里推荐大家使用6.0版本的破解版,可以生成无水印,下方也可以不显示文字. 1.下图是 onbarcode 控 ...
- qsort函数、sort函数
先说明一下qsort和sort,只能对连续内存的数据进行排序,像链表这样的结构是无法排序的. 首先说一下, qsort qsort(基本快速排序的方法,每次把数组分成两部分和中间的一个划分值,而对于有 ...
- oracle排序使用,很多中函数,不同的效果
参考原文:http://blog.csdn.net/wanglipo/article/details/6954915 row_number() OVER (PARTITION BY COL1 ORDE ...
- 转 CAS实现SSO单点登录原理
原文链接 http://m.blog.csdn.net/hxpjava1/article/details/74019017 CAS 简介 1. 1.1. What is CAS ? CAS ( ...
- 读书笔记6pandas简单使用
一.序列Series,很像numpy中的array数组,可以由列表.元组.字典.numpy中的array来初始化 >>> from pandas import Series > ...