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. ...
随机推荐
- lupgu P3950 部落冲突
题目链接 luogu P3950 部落冲突 题解 树剖线段树可以 lct还行 代码 #include<cstdio> #include<algorithm> inline in ...
- 【BZOJ-3672】购票 树分治 + 斜率优化DP
3672: [Noi2014]购票 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1177 Solved: 562[Submit][Status][ ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem C. Cargo Transportation 暴力
Problem C. Cargo Transportation 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed ...
- OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播
参考博客 http://blog.chinaunix.net/uid-11344913-id-4665455.html 在osx系统下通过ffmpeg查看设备 ffmpeg -f avfoundati ...
- MikroTik RouterOS 5.x破解工具HunterTik
HunterTik目前网络上流传的版本大概就两个版本2.3.1和2.3.1.1,其实效果基本一致,都是不能破解高版本的RouterOS,比如6.6以后的版本就不行了. 一.安装: 一路回车! 二.可以 ...
- Chrome中使用老的标题栏界面
Chrome 69中启用了新的UI界面,看着更加秀气了. 但新UI一个不好用的地方是标签栏太高了,留给windows标题栏的空白太小,导致拖动窗口位置非常不方便,如下是一个解决方法: 在地址栏输入: ...
- SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic
SW-DP (Serial Wire Debug Port) Analyzer plugin for the Saleae Logic The SW-DP protocol is described ...
- linux-socket connect阻塞和非阻塞模式 示例
~/cpp$ ./connect 192.168.1.234 1234 kkkk block mode: ubuntu 14.04 : time used:21.0.001053s connect ...
- GetBuiltProjectOutputRecursive error running Xamarin Forms iOS on Visual Studio
Seems like I get this weird problem while running Xamarin.iOS on Visual studio. This happened after ...
- Android 数据存储04之Content Provider
Content Provider 版本 修改内容 日期 修改人 V1.0 原始版本 2013/2/25 skywang 1 URI 通用资源标志符(Universal Resource Identif ...