遍历。每次处理两个点,然后找与他们在一条直线上的点,统计并更新,即可。
-
/**
-
* Definition for a point.
-
* struct Point {
-
* int x;
-
* int y;
-
* Point() : x(0), y(0) {}
-
* Point(int a, int b) : x(a), y(b) {}
-
* };
-
*/
-
class Solution {
-
public:
-
int maxPoints(vector<Point> &points) {
-
// IMPORTANT: Please reset any member data you declared, as
-
// the same Solution instance will be reused for each test case.
-
if(points.size()<3) return points.size();
-
int count=0;
-
int max=0;
-
-
for(int i=0;i<points.size()-1;i++)
-
{
-
for(int j=i+1;j<points.size();j++)
-
{
-
int sign=0;
-
int a,b,c;
-
if(points[i].x==points[j].x) sign=1;
-
else
-
{
-
a=points[j].x-points[i].x;
-
b=points[j].y-points[i].y;
-
c=a*points[i].y-b*points[i].x;
-
}
-
int len=0;
-
for(int k=0;k<points.size();k++)
-
{
-
if((0==sign&&a*points[k].y==c+b*points[k].x)||(1==sign&&points[k].x==points[j].x)) len++;
-
}
-
if(len>max) max=len;
-
}
-
}
-
return max;
-
}
-
};
阅读(1557) | 评论(0) | 转发(0) |