Max Points on a Line——数学&&Map
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的更多相关文章
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [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. ...
- 【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 ...
- 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 ...
- 【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 ...
- 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 ...
- [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. ...
- 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 ...
- 【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 ...
随机推荐
- JS获取移动端系统信息(操作系统、操作系统版本、横竖屏状态、设备类型、网络状态、生成浏览器指纹)
function getOS() { // 获取当前操作系统 var os; if (navigator.userAgent.indexOf('Android') > -1 || navigat ...
- angularJS前端分页插件
首先在项目中引入 分页插件的 js 和 css: 在html页面引入 相关js 和 css: 在控制器中引入分页插件中定义的 module[可以打开pagination.js查看,可以看到 其实,在插 ...
- caffe中使用crop_size剪裁训练图片
layer { name: "data" type: "Data" top: "data" top: "label" i ...
- Codeforces Round #337 (Div. 2)B
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- web.xml中出现<servlet-name>default</servlet-name>是什么意思?
转载自:http://blog.csdn.net/hello5orld/article/details/9407905 在web.xml文件中经常看到这样的配置<servlet-name> ...
- linux包安装,解压,压缩,包管理,环境变量
linux 包安装,解压,压缩,包管理 centoscentos上有系统包管理器yum yum的配置一般有两种方式,一种是直接配置/etc目录下的yum.conf文件,另外一种是在/etc/yum.r ...
- [洛谷P3628] [APIO2010]特别行动队
洛谷题目链接:[APIO2010]特别行动队 题目描述 你有一支由 n 名预备役士兵组成的部队,士兵从 1 到 \(n\) 编号,要将他们拆分 成若干特别行动队调入战场.出于默契的考虑,同一支特别行动 ...
- mysql创建用户,并授权
1.创建用户 CREATE USER 'username'@'host' IDENTIFIED BY 'password'; host分下列3种情况:'%' - 所有情况都能访问‘localhost’ ...
- Linux Shell下执行sqlplus
转载自: http://www.cnblogs.com/include/archive/2011/12/30/2307889.html 以下方法解决了在linux下自动的删除创建用户 sqlplus ...
- 【Foreign】Weed [线段树]
Weed Time Limit: 20 Sec Memory Limit: 512 MB Description 从前有个栈,一开始是空的. 你写下了 m 个操作,每个操作形如 k v : 若 k ...