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. POJ 2828 线段树(想法)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 15422   Accepted: 7684 Desc ...

  2. Linux 学会这些基本可以啦

    1,Linux 内壳文件:cat /etc/issue OR /etc/redhat-release ubuntu[apt-get install -y vim] centos[yum install ...

  3. (BFS)poj1465-Multiple

    题目地址 题意可理解为我们有一些给定的元素,要用它们组成数,如果一个长度(x)所有组成的数都不是给定的另一个数(n)的倍数,并且长度为x的数中有模n的不同于长度小于x的数模n的数,那么继续延长这个数的 ...

  4. HiveQL(HiveSQL)跟普通SQL最大区别一直使用PIG,而今也需要兼顾HIVE

    HiveQL(Hive SQL)跟普通SQL最大区别 一直使用PIG,而今也需要兼顾HIVE.网上搜了点资料,感觉挺有用,这里翻译过来.翻译估计不太准确,待自己熟悉HIVE后再慢慢总结. * No t ...

  5. 判断CAD版本

    使用命令: ACADVER ACADVER = "17.2s (LMS Tech)" (只读) CAD2016 ACADVER = "20.1s (LMS Tech)&q ...

  6. Opencv 2.4.10 +VS2010 项目配置记录

    http://blog.csdn.net/scottly1/article/details/40978625?utm_source=tuicool 因为工作需要,小小的研究一下Opencv的图像处理, ...

  7. 使用OpenXML操作Office文档

    使用OpenXML类库, 以编程的方式来访问PowerPoint, Word, Excel等文档, 有时能够为我们批量编辑文档提供方便. 最近项目中遇到的两个任务是: 1. 替换文档中的图片的Alt ...

  8. Python12期培训班-day1-三级菜单代码分享

    #!/usr/bin/env python3 import sys import os zonecode = { '广东省': {'广州市':['越秀区','海珠区','荔湾区','天河区'], '深 ...

  9. 学习笔记:The Log(我所读过的最好的一篇分布式技术文章)

    前言 这是一篇学习笔记. 学习的材料来自Jay Kreps的一篇讲Log的博文. 原文很长,但是我坚持看完了,收获颇多,也深深为Jay哥的技术能力.架构能力和对于分布式系统的理解之深刻所折服.同时也因 ...

  10. xmind的第六天