LeetCode-149. Max Points on a Line [C++][Java]

该博客介绍了LeetCode的一道题目,目标是找出平面上给定点集中最多共线的点数。解决方案使用了哈希映射记录斜率及其出现次数,并考虑了垂直线的情况。提供了C++和Java两种实现方式,包括基于斜率的HashMap方法和利用最大公约数(GCD)优化的HashMap方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode-149. Max Points on a Lineicon-default.png?t=M3K6https://leetcode.com/problems/max-points-on-a-line/

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.

Example 1:

Input: points = [[1,1],[2,2],[3,3]]
Output: 3

Example 2:

Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4

Constraints:

  • 1 <= points.length <= 300
  • points[i].length == 2
  • -10^4 <= xi, yi <= 10^4
  • All the points are unique.

【C++】

class Solution {
public:
    int maxPoints(vector<vector<int>>& points) {
        unordered_map<double, int> hash; // <斜率, 点个数>
        int max_count = 0, same = 1, same_y = 1;
        for (int i = 0; i < points.size(); ++i) {
            same = 1, same_y = 1;
            for (int j = i + 1; j < points.size(); ++j) {
                if (points[i][1] == points[j][1]) {
                    ++same_y;
                    if (points[i][0] == points[j][0]) {++same;}
                } else {
                    double dx = points[i][0] - points[j][0], dy = points[i][1] - points[j][1];
                    ++hash[dx/dy];
                }
            }
            max_count = max(max_count, same_y);
            for (auto item : hash) {
                max_count = max(max_count, same + item.second);
            }
            hash.clear();
        }
        return max_count;
    }
};

【Java】

1. HashMap Method

class Solution {
    public int maxPoints(int[][] points) {
        Map<Double, Integer> hash = new HashMap<>(); // <斜率, 点个数>
        int max_count = 0, same = 1, same_y = 1;
        for (int i = 0; i < points.length; ++i) {
            same = 1; same_y = 1;
            for (int j = i + 1; j < points.length; ++j) {
                if (points[i][1] == points[j][1]) {
                    ++same_y;
                    if (points[i][0] == points[j][0]) {++same;}
                } else {
                    int dx = points[i][0] - points[j][0];
                    int dy = points[i][1] - points[j][1];
                    double k = 0;
                    if (dx != 0) {k = 1.0 * dx / dy;}
                    hash.put(k, hash.getOrDefault(k, 0) + 1);
                }
            }
            max_count = Math.max(max_count, same_y);
            for (Map.Entry<Double, Integer> item : hash.entrySet()) {
                max_count = Math.max(max_count, same + item.getValue());
            }
            hash.clear();
        }
        return max_count;
    }
}

2. GCD/HashMap Method

class Solution {
    public int maxPoints(int[][] points) {
        if (points == null) return 0;
        if (points.length <= 2) return points.length;
        Map<Integer, Map<Integer, Integer>> counts = new HashMap<>();
        int x1, x2, y1, y2;
        int max = 0;
        for (int i = 0; i < points.length; i++) {
            counts.clear();
            x1 = points[i][0];
            y1 = points[i][1];
            for (int j = i + 1; j < points.length; j++) {               
                x2 = points[j][0];
                y2 = points[j][1];
                int yDiff = y2 - y1;
                int xDiff = x2 - x1;
                int gcd = gcd(xDiff, yDiff);
                int dx = xDiff / gcd;
                int dy = yDiff / gcd;
                if(!counts.containsKey(dx)) {counts.put(dx, new HashMap<>());}
                counts.get(dx).put(dy, counts.get(dx).getOrDefault(dy, 0) + 1);
                max = Math.max(max, counts.get(dx).get(dy));
            }
        }
        return max + 1;
    }
    
    private int gcd(int a, int b) {
        if (b == 0) {return a;}
        return gcd(b, a%b);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值