求直线被确定的最大次数
来源:互联网 发布:网络安装检测报告 编辑:程序博客网 时间:2024/05/02 01:20
问题描述:
Givenn points on a 2D plane, find the maximum number of points that lie on the same straight line.
在一个给定的n个点的平面,求在相同的直线最多的点的点数。
class Solution {
public:
int maxPoints(vector<Point> &points){
//k存在
map<double,int> k0[points.size()];
//k不存在
int k1[points.size()] = {0};
int k_maxnum = 0;
vector<Point>::iterator it1 = points.begin();
vector<Point>::iterator it_end = points.end();
int position = 0;
for(; it1 != it_end; it1++)
{
Point p1(it1->x,it1->y);
for(vector<Point>::iterator it2 = points.begin();it2 != it_end;it2++)
{
//同个点
if(it2 == it1)
{
continue;
}
//斜率不存在
else if(it2->x == it1->x)
{
k1[position] += 1;
}
//斜率存在
else
{
Point p2(it2->x,it2->y);
double k = (p2.y - p1.y)/(p2.x - p1.x);
map<double,int>::iterator it3 = (k0+position).find(k);
if( it = map.end() )
{
(k0+position)[k] = 1;
)
else{
(k0+position)[k] = (k0+position)[k]+1;
}
}
}
position++;
}
for(int i1 = 0; i1<points.size();i1++)
{
for(map<double,int>::iterator it4 = k0[i1].begin(); it4 != k0[i1].end();it4++)
{
if(it4->second > k_maxnum)k_maxnum = it4->second;
}
}
//int k1[points.size()] = {0};
for(int i2 = 0; i2<points.size(); i2++)
{
if(k1[i2] > k_maxnum)k_maxnum = k1[i2];
}
return k_maxnum;
}
};
2.
class Solution {
public:
int maxPoints(vector<Point> &points) {
int size = points.size();
if(size == 0)
return 0;
else if(size == 1)
return 1;
int ret = 0;
for(int i = 0;i<size;i++){
int curmax = 1;
map<double,int>mp;
int vcnt = 0; //垂直点
int dup = 0; //重复点
for(int j = 0;j<size;j++){
if(j!=i){
double x1 = points[i].x - points[j].x;
double y1 = points[i].y - points[j].y;
if(x1 == 0 && y1 == 0){ //重复
dup++;
}else if(x1 == 0){ //垂直
if(vcnt == 0)
vcnt = 2;
else
vcnt++;
curmax = max(vcnt,curmax);
}else{
double k = y1/x1; //斜率
if(mp[k] == 0)
mp[k] = 2;
else
mp[k]++;
curmax = max(mp[k],curmax);
}
}
}
ret = max(ret,curmax+dup);
}
return ret;
}
};
- 求直线被确定的最大次数
- 在多个点中找斜率最大的两个点确定的直线 nlg(n)
- JAVA求 出现次数最多数值最大的数
- SDNU1331 二分法求,确定划分次数后的最小连续区间和
- 两点确定一条直线,已知四个点确定的两条直线,求这两条直线的交点
- vtk 两点确定一条直线的可视化
- 确定空间直线延长线上的一点
- 平面上N个点,没两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点
- 平面上N个点,每两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点
- 平面上N个点,没两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点
- 平面上N个点,每两个点都确定一条直线, 求出斜率最大的那条直线所通过的两个点
- 平面上N个点,每两个点都确定一条直线, 求出斜率最大的那条直线所通过的两个点
- 平面上N个点,每两个点都确定一条直线,求出斜率最大的那条直线所通过的两个点
- 经典算法100道(5)-求确定范围的最大约数
- 经典算法100道(5)-求确定范围的最大约数
- 求两直线的交点
- 求两直线的交点
- 求两直线的交点
- 【第二十二课】容器---Set和HashSet的使用
- c++关于排序与检索的一些简单东西
- 直接插入排序算法实现
- Servlet
- ssh scp等远程操作取消交互认证
- 求直线被确定的最大次数
- SCR888 Slot Game A Night Out Online Casino
- 如何找到Mat中的最大值以及最小值
- 设计模式:抽象工厂模式|菜鸟教程
- Leetcode128——Longest Consecutive Sequence
- Linux多线程——使用互斥量同步线程(2)
- Django
- Android测试(二)
- 安装Android Studio