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 ...
随机推荐
- My Game --线段数据
在背景中用到了一个自定义的类 VectArr : class VectArr { public: VectArr( const Bezier & bz, int conut = 30 ) : ...
- js禁用回退键backspace解决办法
本文摘自http://q821424508.iteye.com/blog/1587025 以下为2.0版本,支持IE,firefox,chrome[这三款浏览器经过测试]等浏览器 window.onl ...
- c#中各类日期的计算方法,收藏
DateTime startWeek = dt.AddDays(1 - Convert.ToInt32(dt.DayOfWeek.ToString("d"))); //本周周一D ...
- Unity3D实现摄像机视野的拉远拉近和跟随主角旋转效果
在Unity官网教程SurvivalShooter(恶魔射手)中,只处理了主角跟随鼠标旋转,摄像机视野并没有旋转或通过滚轮实现视野的拉远拉近,一下是我的实现方法. 在教程中,主角的移动是通过 ...
- 判断表字段是否存在default约束
sql语句 IF NOT EXISTS ( SELECT * FROM dbo.syscolumns WHERE id = OBJECT_ID('[dbo].[TActScoreReceiveRec] ...
- UICollectionView 图片横向滑动居中偏移量的解决
1.在使用UICollectionView 来显示横向滑动图片的时候,cell与cell之间有间隙,每次滑动后cell都会向左偏移,在使用过这两个方法才解决每次向左偏移的部分. 2.使用方法前不要开启 ...
- Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 图论--最近公共祖先问题(LCA)模板
最近公共祖先问题(LCA)是求一颗树上的某两点距离他们最近的公共祖先节点,由于树的特性,树上两点之间路径是唯一的,所以对于很多处理关于树的路径问题的时候为了得知树两点的间的路径,LCA是几乎最有效的解 ...
- 在笔记本电脑开通无线WIFI
1.Windows + R启动运行,输入services.msc进入服务 2.在服务中将Security Center服务从自动启动转为禁止启动 3.在服务中将Windows Firewall的启动类 ...
- Win7重装后,如何删除cygwin目录?
参考: http://blog.csdn.net/zjjyliuweijie/article/details/6577037 http://blog.csdn.net/huangzhtao/artic ...