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

来源:互联网 发布:网络机顶盒 直播 编辑:程序博客网 时间:2024/05/13 21:14

原题网址: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;    }}


0 0
原创粉丝点击