leetCode 149. Max Points on a Line

来源:互联网 发布:linux查看mysql库 编辑:程序博客网 时间:2024/04/24 00:19

很简单,看线段的斜率就可以了。

/** * 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) {        if(points.empty())return 0;        else if(points.size()==1)return 1;                int res = 0;        for(int i=0;i<points.size();i++){            int curmax = 1;            unordered_map<double,int>kcnt;            int vcnt = 0;            int dup = 0;            for(int j=0;j<points.size();j++){                if(j!=i){                   double deltax = points[i].x-points[j].x;                   double deltay = points[i].y-points[j].y;                   if(deltax==0&&deltay==0)dup++;                   else if(deltax==0){                         if(vcnt==0)                            vcnt+=2;                          else                             vcnt++;                          curmax =max(curmax,vcnt);                                           }                   else {                        double k = deltay/deltax;                        if(kcnt[k]==0)                            kcnt[k]=2;                                               else                            kcnt[k]++;                       curmax = max(curmax,kcnt[k]);                   }                }            }            res = max(res,dup+curmax);        }                return res;    }};


0 0
原创粉丝点击