Max Points on a Line leetcode java
题目:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
题解:
这道题就是给你一个2D平面,然后给你的数据结构是由横纵坐标表示的点,然后看哪条直线上的点最多。
(1)两点确定一条直线
(2)斜率相同的点落在一条直线上
(3)坐标相同的两个不同的点 算作2个点
利用HashMap,Key值存斜率,Value存此斜率下的点的个数。同时考虑特殊情况,如果恰巧遍历到一个相同坐标的点,那么就维护一个local的counter来记录相同的点。
维护一个localmax,计算当前情况下的最大值;再维护一个全局Max来计算总的最大值。
返回全局Max即可。
代码如下:
1 public int maxPoints(Point[] points) {
2 if(points.length == 0||points == null)
3 return 0;
4
5 if(points.length == 1)
6 return 1;
7
8 int max = 1; //the final max value, at least one
9 for(int i = 0; i < points.length; i++) {
HashMap<Float, Integer> hm = new HashMap<Float, Integer>();
int same = 0;
int localmax = 1; //the max value of current slope, at least one
for(int j = 0; j < points.length; j++) {
if(i == j)
continue;
if(points[i].x == points[j].x && points[i].y == points[j].y){
same++;
continue;
}
float slope = ((float)(points[i].y - points[j].y))/(points[i].x - points[j].x);
if(hm.containsKey(slope))
hm.put(slope, hm.get(slope) + 1);
else
hm.put(slope, 2); //two points form a line
}
for (Integer value : hm.values())
localmax = Math.max(localmax, value);
localmax += same;
max = Math.max(max, localmax);
}
return max;
}
Reference:
http://blog.csdn.net/ttgump/article/details/23146357
http://blog.csdn.net/linhuanmars/article/details/21060933
Max Points on a Line leetcode java的更多相关文章
- [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: 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 ...
- 【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】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. ...
- [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. ...
- 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 lin ...
- 【Max Points on a Line 】cpp
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- Java for 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. ...
随机推荐
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...
- 使用 IntraWeb (19) - 基本控件之 TIWTreeView
这是个饱受非议的控件; 我通过尝试, 理解了非议, 也能理解作者. 总之向作者的思路靠拢吧, 还是不错的. TIWTreeView 所在单元及继承链: IWCompTreeview.TIWTreeVi ...
- 如何用万用表判断一个12V蓄电池是否没电
常用的方法如下: 1.找一根细铜丝,接触电瓶的正负极柱,冒火花说明电瓶有电,不冒火花说明没有电. 2.用万用表测量电瓶的电压,12.7V说明满电,11.50V一下说明没有电. 3.找一个12V的灯泡或 ...
- oracle 树型结构数据的查询
Oracle中start by prior子句用法 connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start with 条件1 con ...
- HDU 4122 Alice's mooncake shop (RMQ)
Alice's mooncake shop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- [翻译] 10 个实用的 Git 高级命令
1. 输出最后一次提交的改变 这个命令,我经常使用它 来发送其他没有使用 git 的人来检查或者集成所修改的.它会输出最近提交的修改内容到一个 zip 文件中. git archive -o ../u ...
- [golang 易犯错误] golang 局部变量初始化:=的陷阱
我们知道,golang中局部变量初始化方法(使用“:=”创建并赋值),让我们在使用变量时很方便.但是,这也是易犯错误的地方之一.特别是这个初始化符还支持多个变量同时初始化,更特别的是它还支持原有变量赋 ...
- intel32指令中文版
http://scc.qibebt.cas.cn/docs/optimization/VTune(TM)%20User's%20Guide/mergedProjects/analyzer_ec/mer ...
- 利用Android Lost通过互联网或短信远程控制安卓设备
利用Android Lost通过互联网或短信远程控制安卓设备 作者:Jack Wallen| 杰克·瓦伦翻译:PurpleEndurer.2014-11-15第1版 使用智能手机要考虑的一个至关重要的 ...
- 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(3)
chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...