[LC] 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.
Example 1:
Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:
^
|
| o
| o
| o
+------------->
0 1 2 3 4
Example 2:
Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation:
^
|
| o
| o o
| o
| o o
+------------------->
0 1 2 3 4 5 6
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
class Solution {
public int maxPoints(int[][] points) {
if (points == null || points.length == 0) {
return 0;
}
int count = 0;
for (int i = 0; i < points.length; i++) {
Map<String, Integer> map = new HashMap<>();
int samePoints = 0;
int sameX = 1;
for (int j = 0; j < points.length; j++) {
if (j != i) {
if (points[j][0] == points[i][0] && points[j][1] == points[i][1]) {
samePoints += 1;
}
if (points[j][0] == points[i][0]) {
sameX += 1;
continue;
}
int denominator = points[i][0] - points[j][0];
int numerator = points[i][1] - points[j][1];
int gcd = getGCD(numerator, denominator);
String hashStr = (numerator / gcd) + "/" + (denominator / gcd);
map.put(hashStr, map.getOrDefault(hashStr, 1) + 1);
count = Math.max(count, map.get(hashStr) + samePoints);
}
}
count = Math.max(count, sameX);
}
return count;
}
private int getGCD(int a, int b) {
if (a == 0) {
return b;
}
return getGCD(b % a, a);
}
}
[LC] 149. Max Points on a 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]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. ...
- 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 ...
- 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. ...
- 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数
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 --------- java
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 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 ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- [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. ...
随机推荐
- ADFS 4.0 证书更新
ADFS 4.0 证书更新 由于公网证书的过期,需要重新更新ADFS的服务通信证书: 证书要求: 带私钥 PFX格式 更换流程: 证书安装到 证书\计算机\个人,安装后点开证书能看到"你有一 ...
- vue表单选项框
选项框选的内容在下面显示 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- 关于PHP索引数组unset某key后json_encode相关问题踩坑记录
<?php $a = [1,2,3]; var_dump(json_encode($a)); #string(7) "[1,2,3]" unset($a[0]); var_d ...
- Codeforces Round #620 (Div. 2)F2
题意:给出n,和m表示有n天,m块区域,每块区域都有一定数论的动物数量,k表示可以在这一天中观察[x,max(x+k-1,m)]的区域内的动物,有俩台相机,一台只能在偶数天用,另一台则是在奇数天用,每 ...
- h5-其他伪元素
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ModernCNN
# 深度卷积神经网络(AlexNet) LeNet: 在大的真实数据集上的表现并不尽如⼈意. 1.神经网络计算复杂. 2.还没有⼤量深⼊研究参数初始化和⾮凸优化算法等诸多领域. 机器学习的特征提取:手 ...
- os简介
1. 操作系统(Operation System,OS) 操作系统作为接口的示意图  没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自己所编写的程序,就必须用机器语言书写程序 如 ...
- Julia1.x安装
删除julia-1.0 $ jupyter kernelspec list Available kernels: julia-1.0 /Users/keke.zhaokk/Library/Jupyte ...
- [原]你知道怎么使用DebugView查看内核调试信息吗?
原总结注册表sysinternalsdebugviewprocess explorerprocess monitor 简介 DebugView是sysinternals工具集中的一款用来查看调试信息的 ...
- 题解 P3117 【[USACO15JAN]牛的矩形Cow Rectangles】
暴力什么的就算了,贪心他不香吗 这题其实如果分开想,就三种情况需要讨论:(由于不会发图,只能手打) 1) 5 . . . . . 4 . . . . . 3 . . . H . 2 . . G . . ...