https://oj.leetcode.com/problems/max-points-on-a-line/
给n多个二维平面上的点,求其中在同一条直线上点的最大数
细节题,也挺考脑力的
class Solution { public: int maxPoints(vector<Point> &points) { if(points.size() == 0 || points.size() == 1 || points.size() == 2) return points.size(); map<double,int> lines; int ans = 0; for(int i = 0; i< points.size(); i++) { lines.clear();//only use 斜率 represent a line, because from point i, they have a same point, so they are the same lines.insert(make_pair(INT_MIN,1)); //如果只有相同的点的时候,应该把点 i 算在内 int dup = 0; for(int j = i + 1; j< points.size(); j++) { if((points[i].x == points[j].x) && (points[i].y == points[j].y)) { dup++; //record the same point with points[i], then every line + 1 continue; } //calc lines double _k = ((points[i].x == points[j].x) ? INT_MAX : (points[i].y - points[j].y)*1.0/(points[i].x - points[j].x)); if(lines.count(_k)!=0) lines[_k]++; else lines.insert(make_pair(_k,2)); } for(map<double,int>::iterator itr = lines.begin(); itr != lines.end(); itr++) { if(ans < dup + itr->second) ans = dup + itr->second; } } return ans; } };