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 ...
随机推荐
- Linux 编译 websocket++
下载boost 库wget -O boost_last.zip http://downloads.sourceforge.net/project/boost/boost/1.58.0/boost_1_ ...
- DataTable的筛选,过滤后绑定数据源的两种方法(DataTable的select和使用linq返回List集合)
一般数据处理使用DataTable的情况会很多,而我们很多时候会对得到的DataTable的数据进行筛选后绑定到Combobox.GridView.Repeat等控件中,现在分享一下两种DataTab ...
- Qt msgBox 快捷版
使用 int iRet = msgBox(pos, tr("警告") , tr("确定要删除当前选中的行吗?") , tr(, ); == iRet) retu ...
- ASP.NET Cookie存值问题
想必 用Cookie存值已经是很普遍的了,我也是刚学习了一点皮毛,现在就记下一点知识,便于日后翻阅. 1.C#代码存取Cookie值 //用Request获取到客户端Cookie 判断是否为空 if ...
- C#—WebService
一.qq是否在线 1.添加Web引用 qqOnlineWebService cn.com.webxml.www.qqOnlineWebService shelly1 = new NIIT1109 ...
- SQL分类
SQL(Structure Query Language)结构化查询语言,是使用关系型数据库的应用语言. SQL主要可以划分为以下三个类别: DDL(Data Define Language)语句:数 ...
- 表单验证 jQuery Validate
http://www.runoob.com/jquery/jquery-plugin-validate.html http://www.cnblogs.com/linjiqin/p/3431835.h ...
- HyperV上的Linux虚拟机启动报panic_early_exception错误
在Windows Server 2012服务器上的HyperV安装的Oracle Linux 6.4版本,在增加内存容量后(有16G升至30G),操作系统无法启动.黑屏并报"PANIC: e ...
- 关于 Unix 用户权限及进程权限及 Saved set-user-id
最近在看APUE,看到3.14节,fcntl的时候#include <fcntl.h>int fcntl(int fd, int cmd, .../* int arg */);出错返回-1 ...
- firefox 不识别background-position-y / background-position-x
火狐不识别background-position-y 或background-position-x; 案例: 页面: 背景图: 一列按钮,点击时让当前背景图的background-position-y ...