/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
//此题不用斜率存入Map,因为double的斜率也还是会不准确,用最大公约数把x,y处理了,好让它们不会越界。
class Solution {
public int maxPoints(Point[] points) {
if(points == null || points.length == 0) {
return 0;
}
Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
int n = points.length, result = 0;
for(int i = 0; i < n; i ++) {
int max =0, samePoint = 0;
map.clear();//漏了这一步,一定记得要清空map
for(int j = i + 1; j < n; j ++) {
int x = points[i].x - points[j].x;
int y = points[i].y - points[j].y;
if(x == 0 && y == 0) {
samePoint ++;
continue;
}
int gcd = getGcd(x, y);
x = x / gcd;
y = y / gcd;
if(map.containsKey(x)) {
if(map.get(x).containsKey(y)){
map.get(x).put(y, map.get(x).get(y) + 1);
}else {
map.get(x).put(y,1);
}
}else{
Map<Integer,Integer> temp = new HashMap<>();
temp.put(y, 1);
map.put(x, temp);
}
max = Math.max(max, map.get(x).get(y));//此处只要得到map里斜率最大的,不要加上共点。
}
result = Math.max(result, max + samePoint + 1);//此处才需要加上共点们。
}
return result;
}
public int getGcd(int a, int b) {
if(b == 0) {
return a;
}
return getGcd(b, a % b);
}
}