Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

解题思路:

本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历points[i]后面每个点,看看和points[i]的斜率是否出现过,这里有个问题,如何储存斜率,理想状况下,斜率应该用一个String表示,如(0,0)和(2,4)可以存储为"1k2"这样的类型,这会涉及到求最大公约数的问题,实现起来比较麻烦,实际上直接用double表示即可通过,这是因为((double)1/((double)Integer.MAX_VALUE-(double)Integer.MIN_VALUE))是能输出2.3283064370807974E-10的。因此,我们直接用double即可。

JAVA实现如下:

    public int maxPoints(Point[] points) {
if (points.length <= 2)
return points.length;
int max = 2;
for (int i = 0; i < points.length; i++) {
int pointMax = 1, samePointCount = 0;
HashMap<Double, Integer> slopeCount = new HashMap<Double, Integer>();
Point origin = points[i];
for (int j = i + 1; j < points.length; j++) {
Point target = points[j];
if (origin.x == target.x && origin.y == target.y) {
samePointCount++;
continue;
}
double k;
if (origin.x == target.x)
k = Float.POSITIVE_INFINITY;
else if (origin.y == target.y)
k = 0;
else
k = ((double) (origin.y - target.y))
/ (double) (origin.x - target.x);
if (slopeCount.containsKey(k))
slopeCount.put(k, slopeCount.get(k) + 1);
else
slopeCount.put(k, 2);
pointMax = Math.max(pointMax, slopeCount.get(k));
}
max = Math.max(max, pointMax + samePointCount);
}
return max;
}

Java for LeetCode 149 Max Points on a Line的更多相关文章

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

  2. leetcode 149. Max Points on a Line --------- java

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

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

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

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

  6. [LeetCode OJ] Max Points on a Line

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

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

  8. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

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

随机推荐

  1. [转]window.opener用法

    window.opener 实际上就是通过window.open打开的窗体的父窗体. 比如在父窗体parentForm里面 通过 window.open("subForm.html" ...

  2. BIEEE 创建多维钻取分析(4)

    在上一节时,我们创建了一个基于部门号的工资分类汇总. 这里就引出了一个概念:维度 专业的解释大家自行百度,这里就不班门弄斧了.从数据的使用角度看,维度可以简单的理解成“数据分类汇总的一种依据”. 按“ ...

  3. Redis Installation、Configuration、Program Based On Redis Learning

    目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...

  4. 初次使用erlang的concurrent

    如果不是它骇人听闻的并行性能,几乎不会考虑去学习这么一门语言.因为它的并行,我看到的是一块用软件写出来的电路板,是的,它几乎就是把电脑变成了一个可以自由编写逻辑的芯片. 例程来自这里:http://w ...

  5. DLUTOJ 1142 高中的公式

    传送门 Time Limit: 1 Sec  Memory Limit: 128 MB Description 据说...高中学习了好多公式.所以...萌学长不知道该用什么公式来解决下面这个问题.对于 ...

  6. 代码重构-1 对参数中有 bool值的拆分

    最近对一个同事的代码进行重构 第1步 对参数中有 bool值的拆分 原代码如下: private bool CheckIsInFreeTimes(GetDataForValidateLotteryRe ...

  7. MyEclipse------遍历某个路径下的(所有或特定)文件和目录

    usebean包(自己定义的,在src文件夹下面)里的java文件 FileAccept.java package usebean; import java.io.File; import java. ...

  8. html input file标签的上传文件 注意点

    文件上传框  代码格式:<input type=“file” name=“...” size=“15” input enctype="multipart/form-data“ maxl ...

  9. iOS 刚刚,几分钟前,几小时前,几天前,几月前,几年前

    - (NSString *)compareCurrentTime:(NSDate*) compareDate { NSTimeInterval timeInterval = [compareDate ...

  10. SQL 两种表复制语句

    1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...