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 ...
随机推荐
- React中的高阶组件,无状态组件,PureComponent
1. 高阶组件 React中的高阶组件是一个函数,不是一个组件. 函数的入参有一个React组件和一些参数,返回值是一个包装后的React组件.相当于将输入的React组件进行了一些增强.React的 ...
- Educational Codeforces Round 48 (Rated for Div. 2) CD题解
Educational Codeforces Round 48 (Rated for Div. 2) C. Vasya And The Mushrooms 题目链接:https://codeforce ...
- Codeforces Round #508 (Div. 2) E. Maximum Matching(欧拉路径)
E. Maximum Matching 题目链接:https://codeforces.com/contest/1038/problem/E 题意: 给出n个项链,每条项链左边和右边都有一种颜色(范 ...
- Sightseeing(dijlstar) 计算最短路和次短路的条数
Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10004 Accepted: 3523 Desc ...
- Aidl实现进程间通信,跨进程回调
aidl支持的文件类型 1. Java 编程语言中的基本类型, 如 int.long.boolean 等, 不需要 import. 2. String.List.Map 和 CharSequence, ...
- mesos安装、配置和运行
环境:rhel6.6 安装: 安装依赖包 yum install -y tar wget git which nss yum groupinstall -y "Development Too ...
- [实战篇入门]02-POI简单创建Excel
周日的小讲堂要讲到这里,趁中午时间写点东西,记录昨天晚上完成的东西,在这里只是简单的介绍如何创建对于样式问题,我不过多的说,因为之后的教程会使用模版方式搞定! 在学习这段代码的时候,希望各位访问Apa ...
- linux内网机器如何查看本地外网ip
参考自:http://www.gaojinbo.com/linux%E5%86%85%E7%BD%91ip%E7%9A%84%E6%9C%BA%E5%99%A8%E5%A6%82%E4%BD%95%E ...
- [LeetCode] 大数问题,相加和相乘,题 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [Luogu 2590] ZJOI2008 树的统计
[Luogu 2590] ZJOI2008 树的统计 裸树剖不解释. 比板子还简单. #include <algorithm> #include <cstdio> #inclu ...