題目描述
對於給定的n個位於同一二維平面上的點,求最多能有多少個點位於同一直線上
注意處理重合的點,再就是暴力枚舉了
i,j,k 大小單調就可以,這樣不會重複
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
int n = points.length;
if(n < 3) return n;
int ans = 2, tmp = 2, tmp2 = 0;
for(int i = 0; i < n; ++i) {
tmp2 = 0;
for(int j = i+1; j < n; ++j) {
if(points[i].x == points[j].x && points[i].y == points[j].y) {
tmp2++;
continue;
}
tmp = 2;
for(int k = j+1; k < n; ++k) {
int x1 = (points[k].x-points[j].x), y1 = (points[k].y-points[j].y);
int x2 = (points[j].x-points[i].x), y2 = (points[j].y-points[i].y);
if(y1*x2 == x1*y2) tmp++;
}
ans = Math.max(ans, tmp+tmp2);
}
ans = Math.max(ans, 1+tmp2);
}
return ans;
}
}