[LeetCode] 149. Max Points on a Line
来源:互联网 发布:档案软件 编辑:程序博客网 时间:2024/04/26 13:55
难度等级:hard
题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
题解:需要两重循环,第一重循环遍历起始点a,第二重循环遍历剩余点b。
/**
* 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()==0)
return 0;
if(points.size()==1)
return 1;
int res=0;
for(int i=0;i<points.size();i++){
int curmax=1;
map<double,int> m;
int vcnt=0;//垂直点
int dup=0;//重复点
for(int j=0;j<points.size();j++){
if(i!=j){
double y1=points[i].y-points[j].y;
double x1=points[i].x-points[j].x;
if(x1==0&&y1==0) //重复点
dup++;
else if(x1==0){
if(vcnt==0)
vcnt=2;
else
vcnt++;
curmax=max(curmax,vcnt);
}
else{
double k=y1/x1;
if(m[k]==0)
m[k]=2;
else
m[k]++;
curmax=max(curmax,m[k]);
}
}
}
res=max(res,curmax+dup);
}
return res;
}
};
- LeetCode 149. Max Points on a Line
- [leetcode] 149. Max Points on a Line
- leetcode 149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- leetcode.149. Max Points on a Line
- LeetCode-149.Max Points on a Line
- leetcode 149. Max Points on a Line
- leetCode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line
- LeetCode 149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- [LeetCode]149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- [LeetCode] 149. Max Points on a Line
- Leetcode 149. Max Points on a Line
- 【LeetCode】149. Max Points on a Line
- Linux 查看CPU个数和磁盘个数
- Qt遇到奇怪的问题--FTH
- ARM汇编基础
- Dev 控件DevExpress.Wpf.Grid.InfiniteGridSizeException
- 英语美文摘抄
- [LeetCode] 149. Max Points on a Line
- SQL及其使用(2)
- Vue目录结构
- 软通动力:致力智慧城市与大数据协同发展
- Leetcode 517. Super Washing Machines
- 树的子结构
- Mockito 使用
- console.dir()和console.log()的区别
- jmeter个人学习之路--参数化设置