class Solution {
int gcd(int y,int x){
return x==0?y:gcd(x,y%x);
}
public:
int maxPoints(vector<vector<int>>& points) {
int n = points.size();
if(n<=2) return n;
int res = 0;
for(int i=0;i<n-1;++i){
unordered_map<string,int> map;
int repeat = 0;
int maxOnePoint = 0;
for(int j=i+1;j<n;++j){
int deltaY = points[i][1]-points[j][1];
int deltaX = points[i][0]-points[j][0];
if(deltaY==0 && deltaX==0){
++repeat;
continue;
}
int g = gcd(deltaY,deltaX);
deltaY /= g;
deltaX /= g;
string slope = to_string(deltaY)+"/"+to_string(deltaX);
++map[slope];
maxOnePoint = max(maxOnePoint,map[slope]);
}
res = max(res,repeat+maxOnePoint+1);
}
return res;
}
};
149. 直线上最多的点数/C++
最新推荐文章于 2024-06-23 00:24:50 发布