题目:
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
//给定二维平面上的n个点,求同一直线上的点的最大个数。
代码:
public class Solution {
public int maxPoints(Point[] points) {
int abx;
int aby;
int bcx;
int bcy;
if(points.length<=2)
return points.length;
int max=2;
for(int i=0;i<points.length;i++){
int num=0;
int temp=1;
for(int j=i+1;j<points.length;j++){
abx=points[i].x-points[j].x;
aby=points[i].y-points[j].y;
if(abx==0&&aby==0){
num++;
}else{
temp++;
for(int k=j+1;k<points.length;k++){
bcx=points[j].x-points[k].x;
bcy=points[j].y-points[k].y;
if(bcx*aby==bcy*abx){
temp++;
}
}
}
if(max<(temp+num)){
max=temp+num;
}
temp=1;
}
}
return max;
}
}