直接暴力破解:
第一种情况:x相同
第二种情况:y相同
第三种情况,有(a,b)(c,d)(e,f),在同一直线的判断依据是
(a-b)/(c-d) == (c-e)/(d-f)
分别计数求最大即可。
class Solution { public: /** * * @param points Point类vector * @return int整型 */ int maxPoints(vector<Point>& points) { // write code here int ret = 0; int temp; map<int,int> x; // 记x相同 map<int, int> y; // 记y相同 Point a, b, c; for (auto p : points) { x[p.x]++; y[p.y]++; } float _x1, _x2, _y1, _y2; for (int i = 0; i < points.size(); ++i) { a = points[i]; for (int j = i+1; j < points.size(); ++j) { temp = 2; b = points[j]; _x1 = a.x - b.x; _y1 = a.y - b.y; if (_y1 == 0) { break; } for (int k = 0; k < points.size(); ++k) { if (k == i || k == j) continue; c = points[k]; _x2 = b.x - c.x; _y2 = b.y - c.y; if (_y2 == 0) { continue; } if (_x1 / _y1 == _x2 / _y2) temp++; } if (temp > ret) ret = temp; } } for (auto p : y) { if (p.second > ret) ret = p.second; } for(auto p:x){ if(p.second > ret) ret = p.second; } return ret; } };