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 to infinity, then a*b/bwill also evaluate to infinity and not to à`)
  • there is a special value NaN that means not a number. (0.0 / 0.0 evaluates to NaN)
  • there are signed zeroes  (+0.0 and -0.0) Signed zeroes are usually not that painful (-0.0 == +0.0 for the primitive types double and float but not for their object wrappers Double and Float)

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 use Math.PI for infinity since Double.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 type int.

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的更多相关文章

  1. 【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 ...

  2. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  3. [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. ...

  4. 【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 ...

  5. 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 ...

  6. [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. ...

  7. 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 ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. XML学习摘要

    XML元素可以在开始标签中包含属性. 属性(Attribute)提供关于元素的额外信息,属性必须加引号. 属性值必须被引号包围,不过单引号和双引号均可,若属性值本身包含双引号,那么有必要使用单引号包围 ...

  2. debug实战:Unmanaged High Memory非托管高内存

    最近又监控到一个高内存的问题,周五下班把系统打开,周末2天没关,周一来看已经涨到5.2G,这次与以往不同,不是.net的内存泄漏,而是非托管引起的. 1. 抓dump,确定高内存的类型 //dump有 ...

  3. C语言基础--循环 递归打印乘法表

    for循环打印乘法表: #include <stdio.h> // for循环打印乘法表 int main(int argc, const char * argv[]) { //矩形 ; ...

  4. WCF开发指南之构建服务

    一. 引言 Windows通讯基础(简称为WCF)是一种SDK,用于让你使用典型的CLR编程结构(例如用于发布和消费服务的类和接口等)来构建Windows面向服务的应用程序.WCF的编程模型是声明性的 ...

  5. poj2387 spfa求最短路

    //Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...

  6. 获取su后执行的脚本的返回值

    错误的方式: # su - testuser -c "/tmp/test.sh; echo $?"Sun Microsystems Inc.   SunOS 5.10      G ...

  7. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. python语言switch-case

    初学python语言,竟然很久才发现python没有switch-case语句,查看官方文档说是可以用if-elseif-elseif....代替. 讲真,这都不是问题.不就是一个条件判断吗.用if- ...

  9. java之框架

    框架有哪些?C++语言的QT.MFC.gtk,Java语言的SSH,php语言的 smarty(MVC模式),python语言的django(MTV模式)等等设计模式有哪些?工厂模式.适配器模式.策略 ...

  10. js响应HTML客户端下拉列表的修改事件

    这个案例对经常写前端程序的人来讲应该比较简单,不过像我这种习惯于后台开发,对前端不熟悉的人来说,还是有参考意义的. 在asp.net里面,经常需要响应下拉列表DropDownList的Selected ...