/**
* 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) {
maxnum=0;
for(int i=0;i<points.size()-1;i++)
{
int samepoint=1;
max<double,int>m;
int thismax=0;
for(int j=i+1;j<points.size();j++)
{
if(points[j].x==points[i].x&&points[j].y==points[i].y)
samepoint++;
else if(points[j].x==points[i].x)
map[INT_MAX]++;
else
{
double slop=(points[j].y*1.0-points[i].y*1.0)/(points[j].x*1.0-points[i].x*1.0);
map[slop]++;
}
}
for(map<double,int>::iterator it=m.begin();it!=m.end();it++)
{
thismax=max(thismax,(*it).second+samepoint);
}
maxnum=max(maxnum,thismax);
}
return maxnum;
}
};
149. Max Points on a Line
最新推荐文章于 2021-06-24 08:43:46 发布