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

【题意】

求二维平面上n个点中,最多共线的点数。

转自:http://blog.csdn.net/doc_sgl/article/details/17103427

这道题思想很简单··············

分析:

任意一条直线都可以表述为

y = ax + b

假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有

y1 = kx1 +b

y2 = kx2 +b

由此可以得到关系,k = (y2-y1)/(x2-x1)。即如果点c和点a的斜率为k, 而点b和点a的斜率也为k,可以知道点c和点b也在一条线上。

取定一个点points[i], 遍历其他所有节点, 然后统计斜率相同的点数,并求取最大值即可。

计算斜率时,注意重合点和x值相同的两个点(数学上称斜率不存在,此时斜率用int的最大值表示)。

/**
* 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) {
unordered_map<float,int> mp;
int maxNum = ;
for(int i = ; i < points.size(); i++)
{
mp.clear();
mp[INT_MIN] = ;
int duplicate = ;
for(int j = ; j < points.size(); j++)
{
if(j == i) continue;
if(points[i].x == points[j].x && points[i].y == points[j].y)
{
duplicate++;
continue;
}
float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);
mp[k]++;
}
unordered_map<float, int>::iterator it = mp.begin();
for(; it != mp.end(); it++)
if(it->second + duplicate > maxNum)
maxNum = it->second + duplicate;
}
return maxNum; }
};

注意:

0、points中重复出现的点。

1、int maxNum = 0;

初始化,以防points.size() ==0的情况。

2、mp[INT_MIN] = 0;

保证poins中只有一个结点,还有points中只有重复元素时,mp中没有元素。这两种极端情况。

3、int duplicate = 1;

duplicate记录重复点的数量,初始化为1,是因为要把当前的点points[i]加进去。

4、float k = points[i].x == points[j].x ? INT_MAX : (float)(points[j].y - points[i].y)/(points[j].x - points[i].x);

计算斜率,如果直线和y轴平行,就取INT_MAX,否则就取(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)

一开始把(float)(points[j].y - points[i].y)/(points[j].x - points[i].x)写做(float)((points[j].y - points[i].y)/(points[j].x - points[i].x))一直就不对,后来才想明白,注意注意!

Max Points on a Line——数学&&Map的更多相关文章

  1. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  2. [LintCode] Max Points on a Line 共线点个数

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

  3. 【LeetCode】149. Max Points on a Line

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  4. LeetCode: Max Points on a Line 解题报告

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  5. 【leetcode】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  6. 149. Max Points on a Line

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  7. [leetcode]149. Max Points on a Line多点共线

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

  8. Max Points on a Line (HASH TABLE

    QUESTIONGiven n points on a 2D plane, find the maximum number of points that lie on the same straigh ...

  9. 【Max Points on a Line 】cpp

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

随机推荐

  1. SFM

    1.相机模型,内参数和外参数矩阵,相机标定: 2.极线约束和本征矩阵:特征点提取与匹配:提取到的特征点计算本征矩阵(五对以上的点)findEssentialMat(),需啊要点对,焦距参数,cx,cy ...

  2. opencv学习笔记二

    1,读取照片(imread()) 2,处理照片(cvtcolor()) 3,命名窗口(namewindow()) 4,显示照片(imshow()) 5,保存照片(imwrite()) #include ...

  3. ITEXT5 Font 'd:\SIMSUN.TTC' with 'Identity-H' is not recognized.

    用 itextsharp 制作PDF文件的时候发生错误 Font 'd:\SIMSUN.TTC' with 'Identity-H' is not recognized. 原本是 BaseFont b ...

  4. Activiti工作流——流程表数据转化

    任务流程部署:  启动流程实例: 请假人完成请假申请: 部门经理完成审批: 总经理审批完成:

  5. vijos 1037 背包+标记

    描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...

  6. 【C++ STL】List

    1.结构 list使用一个double linked list(双向链表)来管理元素. 2. list 能力 list内部结构和vector或deque截然不同,所以与他们的区别: list不支持随机 ...

  7. 持续集成之Jenkins安装部署

    1.系统环境和安装java环境 [root@devops ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 安装java ...

  8. Golang向Templates 插入对象的值

    Go对象可以插入到template中,然后把对象的值表现在template中,你可以一层层的分解这个对象,去找他的子字段,当前对象用'.'来表示,所以当当前对象是一个string的时候,你可以用{{. ...

  9. Git版本回退的最佳方式

    使用git开发的过程中,存在误提交的时候怎么办呢?不用慌张,强大的git提供了两种版本回退的方式,可以让你恢复提交之前的内容: 方式一:reset(不推荐) 通过reset的方式,把head指针指向之 ...

  10. 【BZOJ1085】【SCOI2005】骑士精神 [A*搜索]

    骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 在一个5×5的棋盘上有12个白色的 ...