149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上。
详见:https://leetcode.com/problems/max-points-on-a-line/description/
Java实现:
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
class Solution {
public int maxPoints(Point[] points) {
if (points == null || points.length == 0){
return 0;
}
Map<String, List<Point>> slopes = new HashMap<>();
int res = 0;
for (int i = 0; i < points.length; i++){
//starting from different points, could be same slope but they are not in a line
slopes = new HashMap<String, List<Point>>();
int samePoints = 1;
int max = 0;
for (int j = i + 1; j < points.length; j++){
int deltaY = points[j].y - points[i].y;
int deltaX = points[j].x - points[i].x;
if (deltaY == 0 && deltaX == 0){
samePoints++;
continue;
}
int gcd = getGCD(deltaX, deltaY);
deltaY /= gcd;
deltaX /= gcd;
String slope = deltaY + ":" + deltaX;
if (!slopes.containsKey(slope)){
slopes.put(slope, new ArrayList<Point>());
}
slopes.get(slope).add(points[j]);
max = Math.max(max, slopes.get(slope).size());
}
res = Math.max(res, max + samePoints);
}
return res;
} private int getGCD(int a, int b){
if (b == 0){
return a;
} else {
return getGCD(b, a % b);
}
}
}
参考:https://www.jianshu.com/p/0073d059687d
149 Max Points on a Line 直线上最多的点数的更多相关文章
- Java实现 LeetCode 149 直线上最多的点数
149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...
- Leetcode 149.直线上最多的点数
直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o ...
- 【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 ...
- [Swift]LeetCode149. 直线上最多的点数 | 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. ...
- Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- 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. ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- [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. ...
随机推荐
- 使用forever让node.js持久运行
何为forever?forever可以看做是一个nodejs的守护进程,能够启动,停止,重启我们的app应用. npm install forever -g #安装 forever start app ...
- 今日头条上看到的js面试题和答案
用js判断字符中每个字符出现的次数, 答案是var info = arr.split('').reduce((a,b)=>(console.log(a,b),a[b]++ || (a[b]=1) ...
- 用Delphi7 调用.NET 2.0的WebService 所要注意的问题(Document格式和UTF8编码)
Delphi7 调用VS.NET 2005开发的基于.NET 2.0的WebService时发生了错误.查阅资料 http://www.community.borland.com/article/bo ...
- 控制cms:CMSDropDownList的width
查找了一下kentico内部使用相关控件的代码,发现有2种方式,可以达到效果. 在我们自己的css class定义的地方,在class前面加上.cms-bootstrap .cms-bootstrap ...
- hdu acm 2844 Coins 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目意思:有A1,A2,...,An 这 n 种面值的钱,分别对应的数量是C1,C2,...,C ...
- 识别String类型变量的问题
碰到了android无法识别string的问题 Cursor cursor = db.query(true, "user", new String[]{"id" ...
- Hadoop 三大调度器源码分析及编写自己的调度器
如要转载,请注上作者和出处. 由于能力有限,如有错误,请大家指正. 须知: 我们下载的是hadoop-2.7.3-src 源码. 这个版本默认调度器是Capacity调度器. 在2.0.2-alph ...
- E20180413-hm
skew adj. 斜的,歪的; [数学] 不对称的; [统计学] 歪斜,扭曲; vt. 歪曲; 曲解; 使歪斜; vi. 偏离,歪斜; 斜视; traversal n. 横越,横断物,(横 ...
- 洛谷 - P2774 - 方格取数问题 - 二分图最大独立点集 - 最小割
https://www.luogu.org/problemnew/show/P2774 把两个相邻的节点连边,这些边就是要方便最小割割断其他边存在的,容量无穷. 这种类似的问题的话,把二分图的一部分( ...
- Codeforces 711B 【模拟】
比赛的时候绝壁打麻烦了... 考虑的好麻烦...wa7...还要判断出来的是不是positive的... 好吧..认了.. #include<cstdio> #include <ma ...