Max Points on a Line

来源:互联网 发布:交通大数据应用 编辑:程序博客网 时间:2024/06/08 16:19

 

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

 

1.string 用做key,如果string比较长,会耗费时间

2.在能不用map,能用单个变量操作,尽量别图方便,map查找O(log(n)),耗费空间

 

 

class Solution {public:unordered_map<double, int> ks;ostringstream os;    int maxPoints(vector<Point> &points) {if(points.size() == 0)return 0;int rst = 0;for(int i = 0; i < points.size(); ++i){ks.clear();double INF = std::numeric_limits<double>::max();int m = 0;int repeat = 1;for(int j = i + 1; j < points.size(); ++j){Point tmp = points[j];if((tmp.x == points[i].x && tmp.y == points[i].y)){repeat++;continue;}double k;if(tmp.x == points[i].x){ k = INF;}else{k = (double)(tmp.y - points[i].y)/(tmp.x - points[i].x);//str = kstr(k,"");}ks[k] ++;m = max(m, ks[k]);}rst = max(rst, m + repeat);}return rst;            }};


 

0 0