题目:

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

链接: http://leetcode.com/problems/max-points-on-a-line/

题解:

这道题是旧时代的残党,LeetCode大规模加新题之前的最后一题,新时代没有可以载你的船。要速度解决此题,之后继续刷新题。

主要思路是,对每一个点求其于其他点的斜率,放在一个HashMap中,每计算完一个点,尝试更新global max。

要注意的一些地方是 -

  • 跳过当前点
  • 处理重复
  • 计算斜率的时候使用double
  • 保存斜率时,第一次要保存2个点,并非1个
  • 假如map.size() = 0, 这时尝试用比较max和 duplicate + 1来更新max

Time Complexity - O(n2), Space Complexity - O(n)

/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
if(points == null || points.length == 0)
return 0;
HashMap<Double, Integer> map = new HashMap<>();
int max = 1; for(int i = 0; i < points.length; i++) { //for each point, find out slopes to other points
map.clear();
int duplicate = 0; //dealing with duplicates for(int j = 0; j < points.length; j++) {
if(j == i)
continue;
if(points[i].x == points[j].x && points[i].y == points[j].y) {
duplicate++;
continue;
} double slope = (double)(points[j].y - points[i].y) / (double)(points[j].x - points[i].x); if(map.containsKey(slope)) {
map.put(slope, map.get(slope) + 1);
} else
map.put(slope, 2);
} if(map.size() > 0) {
for(int localMax : map.values())
max = Math.max(max, localMax + duplicate);
} else
max = Math.max(max, duplicate + 1);
} return max;
}
}

二刷:

又做到了这一题。题目很短,很多东西没有说清楚。比如假如这n个点中有重复,这重复的点我们也要算在结果里。比如[0, 0][0, 0]这是两个点。

这回用的方法依然是双重循环,使用一个Map<Double, Integer>来记录每个点到其他点的斜率slope,然后每统计完一个点,我们尝试更新一下globalMax。这里有几点要注意:

  1. 有重复点的情况,这时候我们用一个整数dupPointNum来记录,并且跳过后面的运算
  2. 斜率
    1. 当点p1.x == p2.x时这时我们要设置slope = 0.0,否则map里可能会出现-0.0和0.0两个key
    2. 当点p1.y == p2.y的时候,我们要设置slope = Double.POSITIVE_INFINITY,否则map里可能出现Double.POSITIVE_INFINITY或者Double.NAGETIVE_INFINITY
    3. 其他情况我们可以使用直线的两点式方程算出斜率slope = (double)(p1.y - p2.y) / (p1.x - p2.x)
  3. 计算完一个点之后,我们遍历Map的value()集,跟globalMax比较,尝试更新

有意思的一点是Double.NaN虽然不等于Double.NaN,即(Double.NaN == Double.NaN)返回false。但作为map的key来说却能在查找时返回true,所以2.2也可以设置slope = Double.NaN.

Java:

Time Complexity - O(n2), Space Complexity - O(n)

/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
if (points == null || points.length == 0) return 0;
Map<Double, Integer> map = new HashMap<>(points.length);
int max = 0, len = points.length; for (int i = 0; i < len; i++) {
map.clear();
int dupPointNum = 0;
for (int j = i + 1; j < len; j++) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
dupPointNum++;
continue;
}
double slope = 0.0;
if (points[j].y == points[i].y) slope = 0.0;
else if (points[j].x == points[i].x) slope = Double.POSITIVE_INFINITY;
else slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x); if (map.containsKey(slope)) map.put(slope, map.get(slope) + 1);
else map.put(slope, 2);
}
max = Math.max(max, dupPointNum + 1);
for (int count : map.values()) max = Math.max(max, count + dupPointNum);
}
return max;
}
}

Update:

加入了排序去重,速度更快了一些。

/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
public int maxPoints(Point[] points) {
if (points == null || points.length == 0) return 0;
Arrays.sort(points, (Point p1, Point p2)-> (p1.x != p2.x) ? p1.x - p2.x : p1.y - p2.y);
Map<Double, Integer> map = new HashMap<>(points.length);
int max = 0, len = points.length; for (int i = 0; i < len; i++) {
if (i > 0 && points[i].x == points[i - 1].x && points[i].y == points[i - 1].y) continue;
map.clear();
int dupPointNum = 0;
for (int j = i + 1; j < len; j++) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
dupPointNum++;
continue;
}
double slope = 0.0;
if (points[j].y == points[i].y) slope = 0.0;
else if (points[j].x == points[i].x) slope = Double.POSITIVE_INFINITY;
else slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x); if (map.containsKey(slope)) map.put(slope, map.get(slope) + 1);
else map.put(slope, 2);
}
max = Math.max(max, dupPointNum + 1);
for (int count : map.values()) max = Math.max(max, count + dupPointNum);
}
return max;
}
}

Reference:

https://leetcode.com/discuss/72457/java-27ms-solution-without-gcd

https://leetcode.com/discuss/57464/accepted-java-solution-easy-to-understand

149. Max Points on a Line的更多相关文章

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

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

  3. Java for 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. 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

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

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

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

  7. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

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

  9. [LC] 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. ...

随机推荐

  1. asp.net 中excel 导入数据库

    protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(Sy ...

  2. 第九篇、CSS布局

    <!--css布局 标准流:从上到下 从左到右 脱离标准流:(浮在父控件的最左边或者最右边)(类似ios在window上添加的控件) 1.float: 2.position: absolute( ...

  3. asp:保留两位小数:

    <%=Formatnumber(-6665.8999,3,-1,-1,0)%>(6665.900)一个例子用到了函数Formatnumber()的所有参数:第一个参数(-6665.8999 ...

  4. 数据库E-R模型,数据字典

    概述:实体-联系模型(简称E-R模型) 模型结构: E-R模型的构成成分是实体集.属性和联系集,其表示方法如下: (1) 实体集用矩形框表示,矩形框内写上实体名. (2) 实体的属性用椭圆框表示,框内 ...

  5. ajax和jsonp的封装

    一直在用jQuery的ajax,跨域也是一直用的jQuery的jsonp,jQuery确实很方便,$.ajax({...})就可以搞定. 为了更好的理解ajax和jsonp,又重新看了下书,看了一些博 ...

  6. Java的基本数据类型

    java的基本数据类型是四类八种: 整型 byte  1字节  8位 short 2字节 16位 int 4字节 32位 long   8字节 64位 在hibernate自动映射中会根据数字长度,选 ...

  7. mina2.0 spring

    Apache MINA是一个网络应用程序框架,它可以帮助用户开发的高性能.高扩展性的网络应用程序.它提供了一个抽象的事件驱动的异步API在不同传输如TCP/IP和UDP/IP通过java NIO. A ...

  8. Java实战之02Hibernate-05检索策略、检索方式

    十一.Hibernate的检索策略 1.概述: 查询的时机:什么时候去查? /** * 一张表的检索策略我们称之为: * 类级别的检索策略. * 注意:只要是说类级别的检索策略,就一定不涉及关联对象. ...

  9. 九度OJ 1501 最大连续子序列乘积 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1501 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含 ...

  10. LeetCode FindMinimuminRotatedSorteArray &&FindMinimuminRotatedSorteArray2

    LeetCode上这两道题主要是使用二分搜索解决,是二分搜索算法的一个应用,根据条件每次舍弃一半,保留一半. 首先第一题: FindMinimuminRotatedSorteArray(时间复杂度为二 ...