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*b
evaluates toinfinity
, thena*b/b
will also evaluate to infinity and not to à`)- there is a special value
NaN
that means not a number. (0.0 / 0.0
evaluates toNaN
)- there are signed zeroes (
+0.0
and-0.0
) Signed zeroes are usually not that painful (-0.0 == +0.0
for the primitive typesdouble
andfloat
but not for their object wrappersDouble
andFloat
)To come back to your question, using
Double
as a key in the map is a terrible idea because of rounding problems. You do not need to useMath.PI
for infinity sinceDouble.POSITIVE_INFINITY
is 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 ...
随机推荐
- sC#进阶系列——WebApi 接口参数不再困惑:传参详解
原文:http://www.cnblogs.com/landeanfen/p/5337072.html 一.get请求 对于取数据,我们使用最多的应该就是get请求了吧.下面通过几个示例看看我们的ge ...
- [转]SecureCRT连接主机时,无法从键盘输入
问题: SecureCRT连接主机时,无法从键盘输入 答案: 最近通过超级终端或者SecureCRT连接主机时,都只能读取设备信息,但是无法从键盘输入,进入不了配置状态,后来仔细检查了配置,居然是流控 ...
- HDFS中的checkpoint( 检查点 )的问题
1.问题的描述 由于某种原因,需要在原来已经部署了Cloudera CDH集群上重新部署,重新部署之后,启动集群,由于Cloudera Manager 会默认设置dfs.namenode.checkp ...
- 关于设置border的小技巧
可以在需要的时候,在某个元素下面放一个长或宽为1px,或者你需要的border宽度的 div ,再在这个div 上设置border.按需要调整这个div的位置.
- 跟大牛之间关于hibernate的一些探讨记录
hibernate的工作原理!! 1.读取配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Session 4.创建事务Transcation 5.持久化操作 6.提交事务 ...
- 传智播客JavaWeb day03
ServletContext 这堂课主要讲ServletContext这个web域(可以看得见范围的)对象,web在启动的时候会创建唯一的ServletContext域对象. 作用:1.用来web域共 ...
- USB协议-USB设备的枚举过程
USB主机在检测到USB设备插入后,就要对设备进行枚举了.为什么要枚举?枚举就是从设备读取各种描述符信息,这样主机就可以根据这些信息来加载合适的驱动程序,从而知道设备是什么样的设备,如何进行通信等. ...
- js Math 对象的方法
Math对象与JavaScript其他对象不同,因为用户不能生成所使用对象的副本,相反脚本汇集了简单Math对象的属性和方法.Math对象实际上存在于每个窗口和框架中,但是对脚本没有影响,程序员把这种 ...
- 讨论贴:Sqlserver varbinary 是二进制数据,却是十六进制的表现形式
首先创建一个数据表 CREATE TABLE [dbo].[log_info]( [id] [,) NOT NULL, [info] [varchar]() NULL, [info1] [varbin ...
- jquery 获取浏览器可视窗口大小,滚动条高度
alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height()); //浏览器时下窗口文档的高度 alert($(docum ...