[leetcode] Max Points on a Line
来源:互联网 发布:mac 下载不在dock 编辑:程序博客网 时间:2024/06/05 15:19
Max Points on a Line
以点为中心暴力枚举。斜率相同点共线。
注意:重合点,与y轴平行。
/** * 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.size()<3){ return points.size(); } int res=0; unordered_map<double,int> slope_cnt;//斜率计数 for(int i=0;i<points.size()-1;i++){ slope_cnt.clear(); int samePointNum=0;//与i重合的点 int point_max=1;//与i共线的最大点数 for(int j=i+1;j<points.size();j++){ double slope;//斜率 if(points[i].x==points[j].x){ if(points[i].y==points[j].y){//点重合 ++samePointNum; continue; }else{ slope=std::numeric_limits<double>::infinity();//平行Y轴,斜率无穷大 } }else{ slope=1.0*(points[i].y-points[j].y)/(points[i].x-points[j].x); } int cnt=0; if(slope_cnt.find(slope)!=slope_cnt.end()){//已有记录 cnt=++slope_cnt[slope]; }else{ cnt=2; slope_cnt[slope]=cnt; } if(point_max<cnt){//更新point_max point_max=cnt; } } res=max(res,point_max+samePointNum);// } return res; }};
0 0
- Max Points on a Line | leetcode
- Max Points on a Line LEETCODE
- Leetcode: Max Points on a Line
- LeetCode - Max Points on a Line
- LeetCode 之 Max Points on a Line
- LeetCode | Max Points on a Line
- [LeetCode] Max Points on a Line
- Leetcode: Max Points on a Line
- LeetCode OJ:Max Points on a Line
- Leetcode Max Points on a Line
- Leetcode: Max Points on a Line .
- [Leetcode] Max Points on a Line (Java)
- leetcode:【Max Points on a Line】
- Max Points on a Line - LeetCode
- LeetCode:Max Points on a Line
- [LeetCode] Max Points on a Line
- leetcode - Max Points on a Line
- LeetCode(149)Max Points on a Line
- Cookie
- uva_10025 - The ? 1 ? 2 ? ... ? n = k problem
- msdn之调试杂项
- 再次温习下C\C++的内存分配
- asp.net 单窗体模型
- [leetcode] Max Points on a Line
- object-c 学习总结2
- 黑马程序员---了解开发工具及内部类
- [leetcode] Reorder List
- Android开发工具ADT关联源码与API文档
- go, Getting Started, Hello world
- uva 10733 - The Colored Cubes(置换)
- Fermat’s Chirstmas Theorem
- 对比使用C# unsafe代码和OpenCV进行图像处理的效率(上)