Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
找出二维平面上处于同一条直线上的最大点数
PS: map用法。for(auto pair:slopes)pair.second
1 /** 2 * Definition for a point. 3 * struct Point { 4 * int x; 5 * int y; 6 * Point() : x(0), y(0) {} 7 * Point(int a, int b) : x(a), y(b) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int maxPoints(vector<Point> &points) { 13 int len = points.size(); 14 map<float,int> slopes; 15 int res=0; 16 for(int i=0;i<len;i++){ 17 slopes.clear(); 18 int duplicate=1; 19 for(int j=i+1;j<len;j++){ 20 if(points[i].x==points[j].x&&points[i].y==points[j].y){ 21 duplicate++; 22 } 23 else{ 24 float temp= points[i].x==points[j].x? INT_MAX :(float)(points[i].y-points[j].y)/(points[i].x-points[j].x); 25 slopes[temp]++; 26 } 27 28 } 29 res=max(res,duplicate); 30 for(auto pair:slopes){ 31 res=max(res,pair.second+duplicate); 32 } 33 } 34 return res; 35 } 36 };