-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution0149.java
More file actions
58 lines (50 loc) · 1.4 KB
/
Solution0149.java
File metadata and controls
58 lines (50 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package leetcode;
import java.util.HashMap;
import java.util.Map;
/**
* @author xiangdotzhaoAtwoqutechcommacom
* @date 2019/12/16
*/
public class Solution0149 {
public int maxPoints(int[][] points) {
if (points == null) {
return 0;
}
if (points.length <= 2) {
return points.length;
}
int res = 0;
for (int i = 0; i < points.length; i++) {
Map<String, Integer> map = new HashMap<>();
int overLap = 0, max = 0;
for (int j = i + 1; j < points.length; j++) {
int x = points[i][0] - points[j][0];
int y = points[i][1] - points[j][1];
if (x == 0 && y == 0) {
overLap++;
continue;
}
int gcd = gcd(x, y);
if (gcd != 0) {
x /= gcd;
y /= gcd;
}
String slope = String.valueOf(x) + y;
int count = map.getOrDefault(slope, 0);
count++;
map.put(slope, count);
max = Math.max(max, count);
}
res = Math.max(res, max + overLap + 1);
}
return res;
}
private int gcd(int a, int b) {
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
}