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. ...
随机推荐
- BZOJ4239 : 巴士走读
考虑按时刻从早到晚模拟,计算出 f[i]:到达i点的最晚出发时间 g[i]:为了赶上第i辆车的最晚出发时间 然后将所有到达n号点的巴士按到达时间排序,查询的时候二分查找即可. 时间复杂度$O(n\lo ...
- object-c中NSString与int和float的相互转换
1,字符串拼接 NSString *newString = [NSString stringWithFormat:@"%@%@",tempA,tempB]; 2,字符转int in ...
- 反接保护电路 Reverse Voltage Protection
Reverse Voltage Protection I've long wanted to pull together some reverse polarity protection ideas ...
- 解决Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER
今天在用java与mysql数据库时发现Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COER ...
- AutoMapper在MVC中的运用07-映射在订单场景的例子
本文参考了Taswar Bhatti的博客,他写了<Instant AutoMapper>这本书.遗憾的是,这本电子版书在国内还买不到,也下载不到.也只能从他的有限几篇博文中来窥探一二了. ...
- iOS 实现复选框 checkbox
-(void)checkboxClick:(UIButton *)btn{ btn.selected = !btn.selected;} - (void)viewDidLoad {UIButto ...
- C#编程(七十一)----------DLR ScriptRuntime
包含DLR ScriptRuntime DLR是微软的一个开源项目.为.NET影城程序提供了动态脚本注入支持.DLR构建的功能包含两个方面,一个是共享的动态类型系统,一个是标准的承载模型.但是VS并没 ...
- Oracle 导入导出 dmp 文件
导入dmp文件,需要知道这个dmp文件创建的用户.因此需要先创建用户,并授权给它. (1)用户的创建 首先,以system用户登录Oracle SQL Developer 其次,在sql工作表(可以用 ...
- Informix 常用函数
一.内部函数 1.内部合计函数 1)COUNT(*) 返回行数 2)COUNT(DISTINCT COLNAME) 返回指定列中唯一值的个数 3)SUM(COLNAME/EXPRESSION) 返回指 ...
- Cocos2d-x开源、跨平台的游戏引擎
from://http://blog.linguofeng.com/pages/language/c/Cocos2dx.html Cocos2d-x 开源.跨平台的游戏引擎 一.下载 http://c ...