LeetCode 149. Max Points on a Line(直线上的点)

4 篇文章 0 订阅
1 篇文章 0 订阅
这篇博客探讨了LeetCode第149题,目标是找出2D平面上同一直线上的最大点数。文章介绍了两种解决方法,包括逐点比较斜率的策略和另一种实现技巧。
摘要由CSDN通过智能技术生成

原题网址:https://leetcode.com/problems/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.

方法一:逐点比较斜率。

/**
 * 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) {
        int max = 0;
        for(int i=0; i<points.length && i+max<points.length; i++) {
            Map<Double, Integer> counts = new HashMap<>();
            int originCounts = 1;
            int verticalCounts = 0;
            int notOriginMax = 0;
            for(int j=i+1; j<points.length; j++) {
                int rx = points[j].x-points[i].x;
                int ry = points[j].y-points[i].y;
                if (rx < 0) {
                    rx = -rx;
                    ry = -ry;
                }
                if (rx == 0 && ry == 0) {
                    originCounts ++;
                } else if (rx == 0) {
                    verticalCounts ++;
                } else {
                    Double slope = (double)ry/rx;
                    Integer count = counts.get(slope);
                    if (count == null) count = 1; else count ++;
                    counts.put(slope, count);
                    // System.out.printf("(%d,%d)-(%d,%d), slope=%f, count=%d\n", points[i].x, points[i].y, points[j].x,points[j].y, slope, count);
                    if (count > notOriginMax) notOriginMax = count;
                }
            }
            if (notOriginMax + originCounts > max) max = notOriginMax + originCounts;
            if (verticalCounts + originCounts > max) max = verticalCounts + originCounts;
        }
        return max;
    }
}

方法二:以其中一点为原点,另一点标准化之后作为key。

/**
 * 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 {
    private int gcd(int x, int y) {
        if (x < y) {
            int t = x;
            x = y;
            y = t;
        }
        while (y!=0) {
            int m = x % y;
            x = y;
            y = m;
        }
        return x;
    }
    public int maxPoints(Point[] points) {
        int max = 0;
        for(int i=0; i<points.length && i+max<points.length; i++) {
            Map<String, Integer> counts = new HashMap<>();
            String origin = "0,0";
            counts.put(origin, 1);
            int notOriginMax = 0;
            for(int j=i+1; j<points.length; j++) {
                int rx = points[j].x-points[i].x;
                int ry = points[j].y-points[i].y;
                if (rx < 0) {
                    rx = -rx;
                    ry = -ry;
                }
                if (rx != 0 && ry != 0) {
                    int gcd = gcd(rx, Math.abs(ry));
                    rx /= gcd;
                    ry /= gcd;
                } else if (rx != 0) {
                    rx = 1;
                } else if (ry != 0) {
                    ry = 1;
                }
                String key = rx + "," + ry;
                Integer count = counts.get(key);
                if (count == null) count = 1; else count ++;
                counts.put(key, count);
                // System.out.printf("(%d,%d)-(%d,%d), relative=(%d,%d)\n", points[i].x, points[i].y,points[j].x, points[j].y, rx, ry);
                if ((rx != 0 || ry != 0) && count > notOriginMax) notOriginMax = count;
            }
            int count = counts.get(origin);
            if (notOriginMax + count > max) max = notOriginMax + count;
        }
        return max;
    }
}

方法三:另一点作为Long类型的key。

/**
 * 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 {
    private int gcd(int x, int y) {
        if (x < y) {
            int t = x;
            x = y;
            y = t;
        }
        while (y > 0) {
            int t = y;
            y = x % y;
            x = t;
        }
        return x;
    }
    public int maxPoints(Point[] points) {
        if (points == null) return 0;
        if (points.length <= 2) return points.length;
        int max = 2;
        for(int i=0; i<points.length-1; i++) {
            int same = 1;
            int ox = points[i].x;
            int oy = points[i].y;
            Map<Long, Integer> counts = new HashMap<>();
            for(int j=i+1; j<points.length; j++) {
                int tx = points[j].x - ox;
                int ty = points[j].y - oy;
                if (tx == 0 && ty == 0) {
                    same ++;
                    continue;
                } else if (tx == 0) {
                    ty = 1;
                } else if (ty == 0) {
                    tx = 1;
                } else {
                    if (ty < 0 || (ty == 0 && tx < 0)) {
                        tx = -tx;
                        ty = -ty;
                    }
                    int g = gcd(Math.abs(tx), Math.abs(ty));
                    tx = tx / g;
                    ty = ty / g;
                }
                Long key = (((long)tx - Integer.MIN_VALUE) << 32) + ty - Integer.MIN_VALUE;
                Integer count = counts.get(key);
                if (count == null) count = 1; else count ++;
                counts.put(key, count);
            }
            if (same > max) max = same;
            for(Integer count: counts.values()) {
                if (count + same > max) max = count + same;
            }
            if (max >= points.length - i) return max;
        }
        return max;
    }
}

另一种实现:

/**
 * 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) return 0;
        int max = 0;
        for(int i = 0; i + max < points.length; i++) {
            int original = 1;
            int vertical = 0;
            Map<Long, Integer> slopes = new HashMap<>();
            for(int j = i + 1; j < points.length; j++) {
                if (points[j].x == points[i].x && points[j].y == points[i].y) {
                    original++;
                } else if (points[j].x == points[i].x) {
                    vertical++;
                } else {
                    long slope = 1000000L * (points[j].y - points[i].y) / (points[j].x - points[i].x);
                    Integer count = slopes.get(slope);
                    if (count == null) {
                        count = 1;
                    } else {
                        count++;
                    }
                    slopes.put(slope, count);
                }
            }
            max = Math.max(max, original + vertical);
            for(int count : slopes.values()) {
                max = Math.max(max, original + count);
            }
        }
        return max;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值