[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 same straight line.算法

哈希表法

复杂度

O(N^2) 时间 O(N) 空间, N为点数函数

思路

应知应会:code

  1. 平面里肯定一条直线要两个数据,能够是两个不一样的点(高中数学作法),也能够是一个点加一个斜率(这道题作法)get

  2. 斜率k = (y2 - y1)/(x2 - x1),当 x1 == x2 时,分母为0,斜率为无穷,表示和y轴平行的直线们数学

  3. 在计算机里使用double表示斜率,是不严谨的也是不正确的,double有精度偏差,double是有限的,斜率是无限的,没法使用有限的double表示无限的斜率,不过此题的test case没有涉及这个问题hash

  4. 表示斜率最靠谱的方式是用最简分数,即分子分母都没法再约分了。分子分母同时除以他们的最大公约数gcd便可获得最简分数it

  5. gcd(a,b),通常求的是两个正整数的gcd。这道题a和b有多是0,分别表示与x轴或y轴平行的斜率(注意ab不能同时为0,表示同一个点没有意义),因此这道题咱们规定ab取值范围:a>=0,b>=0。至于负数,先变成正数取gcd,再肯定最终斜率的正负io

  6. gcd ( a , b ) = (b == 0) ? a : gcd ( b , a % b ), a,b是任意非负整数且a,b至少有一个不为0class

  7. 观察gcd(a,b),假设a,b为非负整数:test

    1. a和b中有一个为零,那么gcd为另外一个不为0的数;

    2. a和b都为0,gcd为0;

算法:

没什么算法就是穷举:
对每一个点,都计算一下该点和其余点连线的斜率,这样对于这个点来讲,相同斜率的直线有多少条,就意味着有多少个点在同一条直线上,由于这些直线是相同的。另外,若是计算过点A和点B的直线,当算到点B时,就不用再和A连线了,由于AB这条直线上的点数已经都计算过了。这里,咱们用哈希表,以斜率为key,记录有多少重复直线。

注意

  • 求gcd的utility:

public int gcd(int a, int b) {
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
  • 两层循环外层 i 从 0 到 n, 内层 j 从 i+1 到 n

  • 若是想要自定义Slope(斜率)类做为HashMap的Key的话要重写hashCode()和equals()函数, 偷懒的话能够把斜率的分数表示成String做为Key,这样就省了一些事

  • hashmap的value的含义应定义为:过cur点以key为斜率的直线有几个除了cur之外的点。在算完 过cur的全部直线后,统计cur点的总个数howManyCur,加到当前点最多的直线上,便可获得含cur点的最大直线上的点数

代码

/**
 * 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.length <= 1)
            return points.length;
        int maxUniv = Integer.MIN_VALUE;
        for (int i = 0; i < points.length; i++) {
            Point cur = points[i];
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            int howManyCur = 1, maxLocal = 0;
            for (int j = i + 1; j < points.length; j++) {   //这里能够从i+1开始,以前的都算过了
                    Point iter = points[j];
                    if (iter.x == cur.x && iter.y == cur.y) {//同一顶点
                        howManyCur += 1;
                    } 
                    else {          //不一样顶点
                        String key = getSlopeInString(cur, iter);
                        //map里存(过cur点,斜率key)表明的直线有多少除了cur的点
                        map.put(key, map.containsKey(key) ? map.get(key) + 1 : 1);
                        maxLocal = Math.max(maxLocal, map.get(key));
                    }
            }
            maxLocal = howManyCur + maxLocal;
            maxUniv = Math.max(maxLocal, maxUniv);
        }
        return maxUniv;
    }
    public String getSlopeInString(Point cur, Point iter) {
        int numerator = iter.y - cur.y;
        int denominator = iter.x - cur.x;
        String sign = getSign(numerator, denominator);
        int gcd = gcd(Math.abs(numerator), Math.abs(denominator));//0和任意一个非零数'a'的gcd为'a',0和0的gcd为0,因此斜率为无穷的状况分母为0
        return sign + Math.abs(numerator)/gcd + "/" + Math.abs(denominator)/gcd;
    }
    //a和b为非负整数 且 a和b不一样时为0
    public int gcd(int a, int b) {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
    public String getSign(int a, int b) {
        if (a <= 0 && b <= 0 || a >= 0 && b >= 0)
            return "+";
        else 
            return "-";
    }
}
相关文章
相关标签/搜索