leetcode 149. Max Points on a Line --------- java
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
if (points.length < 3) return points.length; int max = 0;//用于返回的结果,即共线点的最大个数
Map<Double, Integer> map = new HashMap<Double, Integer>();//保存同一个斜率的点的个数 for (int i = 0; i < points.length; i++) {//以每一个点为固定点
map.clear();
int duplicate = 1;//记录跟固定点重合的个数 for(int j = i+1 ; j < points.length; j++){//遍历其他点,求其与固定点之间的斜率
double slope = 0.0;//斜率 if (points[i].x == points[j].x && points[i].y == points[j].y) {//如果跟固定点重合
duplicate++;
continue;
} else if (points[i].x == points[j].x) {//如果跟固定点在同一条竖线上,斜率设为最大值
slope = Integer.MAX_VALUE;
} else if( points[i].y == points[j].y ){//计算该点与固定点的斜率
slope = 0;
} else{
slope = 1.0 * (points[i].y - points[j].y) / (points[i].x - points[j].x);
}
map.put(slope, map.containsKey(slope) ? map.get(slope) + 1 : 1);
} //更新最优解
if (map.keySet().size() == 0) {//如果map为空
max = duplicate > max ? duplicate : max;
} else {
for (double key : map.keySet()) {
max = Math.max((duplicate + map.get(key)), max);
}
}
}
return max;
}
}
leetcode 149. Max Points on a Line --------- java的更多相关文章
- [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. ...
- 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. ...
- [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 line. ...
- 【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 ...
- [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 ...
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- 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 li ...
随机推荐
- opencv+ffmpeg实现avi视频的播放
配了一天,终于成功的在ubuntu上安装了ffmpeg,实现了opencv对avi文件的读取. 在CvCapture* pCapture=cvCaptureFromAVI("video.av ...
- POJ 3648 2-sat
题目大意: 有一对新人结婚,邀请n对夫妇去参加婚礼. 有一张很长的桌子,人只能坐在桌子的两边,还要满 足下面的要求:1.每对夫妇不能坐在同一侧 2.n对夫妇 之中可能有通奸关系(包括男男,男女,女女) ...
- wince6.0 开机启动定制的程序
1.prject.bib MediaApp.exe $(_FLATRELEASEDIR)\MediaApp.exe NK H MediaApp.lnk $(_FLATRELEASEDIR)\Media ...
- android平台手电筒开发源代码
android平台手电筒开发源代码,AndroidManifest.xml文件的入口是startapp,这个文件没上传上来,大家可以自己写. 1. [代码]android 1 2 3 4 5 6 7 ...
- oracle删除数据库中的所有数据的拼接语句
create or replace function count_rows/**查询各表实际记录数*/(table_name in varchar2,owner in varchar2 default ...
- 2016-1-6第一个完整APP 私人通讯录的实现 2:增加提示用户的提示框,监听文本框
一:在登录时弹出提示用户的提示框: 1.使用第三方框架. 2.在登陆按钮点击事件中增加如下代码: - (IBAction)loginBtnClicked { NSString *acount = se ...
- 前端开发者应该知道的 CSS 小技巧
一些小技巧让你的CSS技术更专业 使用:not()去除导航上不需要的边框 为body添加行高 垂直居中任何元素 逗号分离的列表 使用负nth-child选择元素 使用SVG图标 文本显示优化 在纯CS ...
- iptables_forward
FORWARD 好了,我们接着往下走,这个包已经过了两个PREROUTING链了,这个时候,出现了一个分支转折的地方,也就是图中下方的那个菱形(FORWARD),转发!这里有一个对目的地址的判断(这里 ...
- 学生信息管理系统v1.0
昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙.需要统计处理很多学生信息,想让我帮他做一个管理系统.实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到 ...
- 9、网页制作Dreamweaver(jQuery基础:事件)
事件 定义 即当HTML中发生某些事(点击.鼠标移过等)的时候调用的方法 $(selector).action() 触发 事件的触发有两种方法: 1.直接将事件click写在<javascrip ...