/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solution { public: int maxPoints(vector<Point> &points) { int size = points.size(); if (size==0 || size==1){ return size; } int ret = 0;//记录每一次外循环扫描结束后的同线最多点 for (int i=0;i<size;i++){ int curmax = 1;
map<double,int> mp;//构造 斜率,点数的map int vcount = 1;//垂直点 int dup = 0;//重复点 for (int j=i+1;j<size;j++){ double x1 = points[i].x - points[j].x; double y1 = points[i].y - points[j].y; if (x1==0 && y1 ==0){//重复 dup++; }else if (x1 == 0){//垂直 vcount++; curmax = vcount>curmax?vcount:curmax; }else{//有斜率的情况 double k = y1/x1; if (mp[k]==0){ mp[k]=2; }else{ mp[k]++; } curmax = mp[k]>curmax?mp[k]:curmax; } } ret = max(curmax+dup,ret); } return ret; } };