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 ...
随机推荐
- 【STL】-pair的用法
初始化: std::pair<int, float> p; //initialize p.first and p.second with zero std::pair<int, co ...
- Problem A CodeForces 556A
Description Andrewid the Android is a galaxy-famous detective. In his free time he likes to think ab ...
- matlab代码 图像处理源码
非常不错的找图像处理源码的地方,源码搜搜. http://www.codesoso.com/Category.aspx?CategoryId=56
- jquery api调用
本框架内置组件以及部分插件都可以通过jquery选择器进行API调用,支持链式操作,如下示例. <script type="text/javascript"> $(&q ...
- 给伪类设置z-index= -1;
.column{ position: relative; float: left; padding: 30px 0; width: 25%; z-index: 0; background-color: ...
- JVM值内存垃圾回收监控之jstat
如何判断JVM垃圾回收是否正常?一般的top指令基本上满足不了这样的需求,因为top主要监控的是总体的系统资源,很难定位到java应用程序. Jstat是JDK自带的一个轻量级小工具.全称“Java ...
- (转)HTML5开发学习(2):本地存储之localStorage 、sessionStorage、globalStorage
原文:http://www.cnblogs.com/xumingxiang/archive/2012/03/25/2416386.html HTML5开发学习(2):本地存储之localStorage ...
- 2016 - 1- 22 Build a Nav bar (intro to HTML&CSS)
一:Learn how to build a NavBar --- allow user navigate ur site 1. The hypetext refrence link This att ...
- linux添加动态库搜索路径
在有时运行程序出现动态库找不着的问题,而明明装了的.这时候可能是没有将相应的路径添加到系统中去. 具体说:cd /etc/ld.so.conf.d/ 可以发现里面有一堆*.conf的文件 我们要做的就 ...
- 计算几何----判断空间点是否在一个四面体(tetrahedron)内部
DESCRIPTION: 判断空间点 P(x, y, z)是否在一个四面体的内部? Let the tetrahedron have vertices V1 = (x1, y1, z1) V2 = ( ...