10. 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.
主要思想:O(n2),固定一个点,遍历其余 n 个点, 计算与该点相同的点的个数,和其余所有点的斜率,相同斜率的点视为同一直线。
初步 AC 代码:
/**
* 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 maxNumber = 0;
for(size_t i = 0; i < points.size(); ++i) {
int repeat = 1;
int sameX = 0;
for(size_t j = 0; j < points.size(); ++j) {
if(j == i) continue;
if(points[j].x == points[i].x) {
if(points[j].y == points[i].y) ++repeat;
else ++sameX;
}
else {
float k = (float)(points[i].y - points[j].y) / (points[i].x - points[j].x);// key.
mp[k]++;
}
}
unordered_map<float, int>::iterator it = mp.begin();
while(it != mp.end()) {
if(it->second + repeat > maxNumber) maxNumber = it->second + repeat;
++it;
}
maxNumber = (repeat + sameX) > maxNumber ? (repeat + sameX) : maxNumber;
mp.clear();
}
return maxNumber;
}
};
但是里面的 hash map 使用了 浮点值做键值是个非常拙劣的方法。如下所述:
Testing equality with float ou double is always a bad idea because of rounding errors, so don't use them as a hash. Never.
For it's floating point arithmetic, Java uses a subset of IEEE754. IEEE754 is full of beautiful tricks to mimic the behavior of real numbers and do a wonderful job at it, so sometimes we forget that is has limitations. The main troubles are (in no specific order):
- there is a gap between 0.0 and the next value (so non-zero numbers can be rounded to 0 if they are in this gap)
- there are special values for +infinity and -infinity (so there is no overflow, but you can get "stuck" on an infinite value. Imagine
a*bevaluates toinfinity, thena*b/bwill also evaluate to infinity and not to à`)- there is a special value
NaNthat means not a number. (0.0 / 0.0evaluates toNaN)- there are signed zeroes (
+0.0and-0.0) Signed zeroes are usually not that painful (-0.0 == +0.0for the primitive typesdoubleandfloatbut not for their object wrappersDoubleandFloat)To come back to your question, using
Doubleas a key in the map is a terrible idea because of rounding problems. You do not need to useMath.PIfor infinity sinceDouble.POSITIVE_INFINITYis a legitimate value. Be careful though, you don't want to have positive and negative infinity mixed up. Look at the previous questions, the trick here is to represent a line by an equation ax+by+c=0 with a,b, and c of typeint.As a final note, I think it is important to know the limitations of the encoding you are using (overflow for int, signed zeroes, infinity, and NaN of floating point numbers, surrogate pair for UTF-16…)
所以待重新写答案 AC 一次。
10. Max Points on a Line的更多相关文章
- 【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 ...
- [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]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 leetcode java
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- 【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 ...
- 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 lin ...
随机推荐
- Windows 95 vs. Windows 10
- OC中intValue要注意的地方
在程序中,发现一个问题,写了个例子,如下: NSDictionary * dict = [[NSDictionary alloc] init]; NSString * s ...
- Java EE注册三部曲(一步曲)
一步曲(html+servlet+SQL+Bean+Dao+加密(Base64)) 设计思路: 1:编写前台页面jsp:register.jsp,使得用户能够实行注册操作 2:编写servlet:re ...
- 团队开发——冲刺1.b
冲刺阶段一(第二天) 1.昨天做了什么? 在了解C#的基础上,深入熟悉Windows窗体应用程序,熟练掌握基本功能 2.今天准备做什么? 在C#的Windows窗体应用程序中,设计简单的游戏开始主界面 ...
- JQuery正则验证
比较常用的: function checkIshanzi(s) { //var patrn = /^[\u2E80-\u9FFF]$/; //Unicode编码中的汉字范围 /[^\x00-\x80] ...
- Jena TDB 102
1 Introduction TDB is a RDF storage of Jena. official guarantees and limitations TDB support full ra ...
- SPARQL1.1 101 Language and Jena support
1 introduction definition cited from SPARQL 1.1 Overview: SPARQL 1.1 is a set of specifications that ...
- HDU 5950 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- POJ 1511 Invitation Cards (spfa的邻接表)
Invitation Cards Time Limit : 16000/8000ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) ...
- jquery插件文件上传
文件上传有很多jQuery插件,一般我最为常用的就是uploadify.js和ajaxfileupload.js,二者都是以file标签为依托,前者需要在页面初始化时就渲染插件,比较适合单纯的文件上传 ...