Max Points on a Line
public class Solution {
public int maxPoints(Point[] points) {
if(points == null || points.length == 0) return 0;
int max = 1;
for(int i = 0; i < points.length; i++) {
HashMap<Float, Integer> map = new HashMap<Float, Integer>();
int same = 0, local = 0, sameX = 1;
for(int j = 0; j < points.length; j++) {
if(i == j) continue;
if(points[i].x == points[j].x) {
if(points[i].y == points[j].y){
same++;
} else {
sameX++;
}
continue;}
float slope = (float)(points[j].y - points[i].y) / (points[j].x - points[i].x);
if(map.containsKey(slope)) {
map.put(slope, map.get(slope) + 1);
} else {
map.put(slope, 2);
}}
for(int count : map.values()) {
local = Math.max(local, count);
}
local = Math.max(local, sameX);
local += same;
max = Math.max(max, local);
}
return max;
}
}

浙公网安备 33010602011771号