题目链接
u
n
o
r
d
e
r
e
d
unordered
unordered
_
s
e
t
\_set
_set<
l
o
n
g
long
long
d
o
u
b
l
e
double
double,
i
n
t
int
int>记录相同斜率的点的个数,然后与过
x
x
x 轴和
y
y
y 轴点的个数取
m
a
x
max
max
class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
int res = 0;
for(auto& x : points)
{
int x1 = x[0], y1 = x[1];
int h = 0, s = 0, r = 0;
unordered_map<long double,int> hash;
for(auto& y : points){
int x2 = y[0], y2 = y[1];
long double k = (long double)(y2-y1)/(x2-x1);
if(x1 != x2 && y2 != y1) hash[k]++;
if(x1 == x2 && y2 != y1) s++;
if(x1 != x2 && y2 == y1) h++;
if(x1 == x2 && y1 == y2) r++;
}
int f = -1;
for(auto& x : hash)
if(f < x.second) f = x.second;
res = max(res,max(f+r,max(h+r,s+r)));
}
return res;
}
};