给定一个二维平面及平面上的N个点列表Points,其中第i个点的坐标为Points[i] = [Xi, Yi]。找出一条直线,其通过的点的数目最多。
设穿过最多点的直线所穿过的全部点编号从小到大排序的列表为S,你仅需返回[S[0],S[1]]作为答案,若有多条直线穿过了相同数量的点,则选择S[0]值较小的直线返回,S[0]相同则选择S[1]值较小的直线返回。
思路:暴力哈希表
class Solution {
public:
vector<int> bestLine(vector<vector<int>>& points) {
int n = points.size();
vector<int> res(2);
int mxLen = 0;
res[0] = 0, res[1] = 1;
for(int i = 0; i < n; i ++ ){
for(int j = i + 1; j < n; j ++ ){
int tmpLen = 2;
for(int k = j + 1; k < n; k ++ ){
if(((long long)points[j][1] - points[i][1]) * (points[k][0] - points[i][0]) == ((long long)points[k][1] - points[i][1]) * (points[j][0] - points[i][0])){
tmpLen ++ ;
}
}
if(tmpLen > mxLen){
mxLen = tmpLen;
res[0] = i;
res[1] = j;
}
}
}
return res;
}
};