max-points-on-a-line
来源:互联网 发布:清水建设 知乎 编辑:程序博客网 时间:2024/06/04 19:00
leetcode的一道题
在平面内若干个点中,求出在同一直线上点数最多的点数。
用穷举法
/**
* 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;
else if(points.size() == 1) return 1;
int res = 2;
for(int i = 0; i < points.size(); i++){
int sameP = 0;
int vert = 0;
unordered_map<double, int> km;
for(int j = i + 1; j < points.size(); j++){
int dy = points[i].y - points[j].y;
int dx = points[i].x - points[j].x;
if(dx == 0)
{
if(dy == 0){
sameP++;
}
else{
vert++;
}
}
else{
double k = dy / double(dx);
km[k]++;
}
}
int max_n = vert;
for(const auto &temp : km){
if(temp.second > max_n)
max_n = temp.second;
}
max_n = max_n + sameP + 1;
res = (res > max_n ? res : max_n);
}
return res;
}
};
其中的坑点::36行要注意把整数强制转化为浮点数。
我一开始没住意,坑了我半小时,去想是不是哪儿逻辑有问题。
谨记c++的这种坑,以后别犯这种低级错了
1 0
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points On a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- max points on a line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Max Points on a Line
- Fibsieve`s Fantabulous Birthday<规律>
- Rxjava操作步骤
- Openstack并行性能加速
- mysql 主键 外键
- bzoj1500维修序列(splay)
- max-points-on-a-line
- 单链表的整表创建和删除以及顺序存储结构和链式结构的对比
- linux c 编程实战:文件操作总结(一)
- MYSQL函数学习
- HDFS全面解析涉及基础、命令、API
- WPF中自动增加行(动画)的TextBox
- HDU5673->逆元求组合数&&卡特兰数
- CodeForces 427B Prison Transfer
- Android 计算器解析(四): 完善计算功能