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. ...
随机推荐
- android listview 优化
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha android convertview作用 ======= 1,convertView复 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem F. Finance 模拟题
Problem F. Finance 题目连接: http://codeforces.com/gym/100714 Description The Big Boss Company (BBC) pri ...
- OpenVPN的ipp.txt为空不生效的问题
1.ipp.txt是分配固定IP使用的,但在tun模式下里面的ip地址不是写使用着的IP,而是30位子网中没有列举出来的启动一位,比如我要给客户机分配为10.8.0.6的IP,那么它这个文件填写的是1 ...
- oracle 树型结构数据的查询
Oracle中start by prior子句用法 connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start with 条件1 con ...
- ORA-00600: [kck_rls_check must use (11,0,0,0,0) or lower] 故障解决
一朋友在QQ上问我,说他数据库的pfile 和spfile 都不见了.我问他数据库是10g还是11g的,他说11g,所以我就让他用这个语法来创建spfile了: SQL> create spfi ...
- Html学习笔记3
1表格的标题和表头: <table> <caption>成绩单</caption> <tr> <th>姓名</th> <t ...
- 微软收购跨平台移动开发公司Xamarin
摘要:北京时间2月25日早间消息,微软周三宣布收购创业公司Xamarin,这也是该公司为了吸引更多软件工程师为其云计算服务编写程序而采取的最新举措.古斯里表示,总部位于旧金山的Xamarin创立于20 ...
- Icon cache rebuilding with Delphi(Delphi 清除Windows 图标缓存源代码)
清除Windows图标缓存的代码: procedure RebuildIconCache; .... const sr_WindowMetrics='Control Panel\Desktop\Win ...
- Spring Boot 2中对于CORS跨域访问的快速支持
原文:https://www.jianshu.com/p/840b4f83c3b5 目前的程序开发,大部分都采用前后台分离.这样一来,就都会碰到跨域资源共享CORS的问题.Spring Boot 2 ...
- TMS WEB CORE直接从HTML&CSS设计的页面布局
TMS WEB CORE直接从HTML&CSS设计的页面布局 TMS WEB CORE支持DELPHI IDE中拖放控件,生成HTML UI.这种方式适合DELPHI和C++ BUILDER的 ...