您的位置:首页 > 其它

leetcode_Max Points on a Line

2014-05-13 10:27 295 查看
/*
* Max Points on a Line
* */
public int maxPoints(Point[] points)
{
int max = 0;
for(int i=0;i<points.length-1;i++)
{
for(int j=i+1;j<points.length;j++)
{
int num=0;

//if this 2 points are the same , skip this time, or we'll get result 'n' which is probably not correct!
if(points[j].x == points[i].x && points[j].y == points[i].y)
{
continue;
}

for(int k=0;k<points.length;k++)
{
int isLine = (points[i].y-points[k].y)*(points[j].x-points[k].x)-(points[j].y-points[k].y)*(points[i].x-points[k].x);
if(isLine==0)
{
num++;
}
}
if(num>max)
{
max = num;
}
}

}

//if only one or two points, above judgement will get result 0  while the correct result is 1 or 2
if(points.length<=2)
{
max = points.length;
}
else if(max==0)
{
// this case is that all points are the same
max=points.length;
}
return max;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: