题目描述
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
/**
* 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) {
if(points == null)
return 0;
int len = points.length;
if(len<=2)
return len;
int max = 0;
for(int i = 0;i<len; i++){
Point p1 = points[i];
int num = 0; //记录重复点个数
int temp =1;
for(int j = i+1; j<len; j++){
int fenzi=0, fenmu=0, gcd=1;
Point p2 = points[j];
fenzi = p1.y - p2.y;
fenmu = p1.x - p2.x;
if(fenzi==0&&fenmu==0)//重复点
num++;
else{
temp++;
for(int k = j+1; k<len; k++){
Point p3 = points[k];
int fenzi2 = p3.y - p2.y;
int fenmu2 = p3.x - p2.x;
if(fenzi2*fenmu==fenmu2*fenzi)
temp++;
}
}
if(max<(num+temp))
max = temp + num;
temp = 1;
}
}
return max;
}
}