149. 直线上最多的点数

https://leetcode.cn/problems/max-points-on-a-line/?envType=study-plan-v2&envId=top-interview-150

我们可以通过斜率 + 出发点位置来判断多个点是否位于同一条直线,遍历所有点依次选取作为出发点然后遍历其它点计算斜率,并且通过hashmap来记录斜率出现的次数,对于水平线和竖直线我们可以单独拿出来统计,最后比较得出三种情况的最大值。

java 复制代码
    public int maxPoints(int[][] points) {
        int n = points.length;
        if (n <= 2) {
            return n;
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            HashMap<Double, Integer> map = new HashMap<>();
            int row = 1, col = 1; // 记录水平线和竖直线
            int max = 0; // 记录斜率出现次数的最大值
            for (int j = i + 1; j < n; j++) {// 遍历其它点
                if (points[i][0] == points[j][0]) {
                    col++;
                    continue;
                }
                if (points[i][1] == points[j][1]) {
                    row++;
                    continue;
                }
                // 斜线情况
                double k = (double) (points[i][1] - points[j][1]) / (points[i][0] - points[j][0]);
                if(!map.containsKey(k)) {
                    map.put(k, 2);
                } else {
                    map.put(k, map.get(k) + 1);
                }
                max = Math.max(max, map.get(k));
            }
            max = Math.max(max, Math.max(row, col));
            res = Math.max(res, max);
        }
        return res;
    }

我们还可以使用向量叉积来判断是否共线,向量共线条件:dx1 * dy2 == dx2 * dy1

java 复制代码
    public int maxPoints(int[][] points) {
        int n = points.length;
        if(n <= 2) return n;
        int res = 2;
        // 因为points的长度比较小,三层for可以接受
        for(int i = 0; i < n; i++) {
            for(int j = i + 1; j < n; j++) {
                // 选取两个点计算向量
                int x1 = points[i][0], y1 = points[i][1];
                int x2 = points[j][0], y2 = points[j][1];
                int max = 2;
                // 计算向量
                int dx1 = x2 - x1, dy1 = y2 - y1;
                // 找第三点
                for(int k = j + 1; k < n; k++) {
                    // 计算第一点和第三点向量
                    int dx2 = points[k][0] - x1, dy2 = points[k][1] - y1;
                    // 判断之前向量是否共线
                    if(dx1 * dy2 == dx2 * dy1) {
                        // 共线则计数
                        max++;
                        res = Math.max(res, max);
                    }
                }
            }
        }
        return res;
    }
相关推荐
鱼跃鹰飞30 分钟前
Leetcode会员尊享100题:270.最接近的二叉树值
数据结构·算法·leetcode
没差c1 小时前
springboot集成flyway
java·spring boot·后端
时艰.1 小时前
Java 并发编程之 CAS 与 Atomic 原子操作类
java·开发语言
梵刹古音1 小时前
【C语言】 函数基础与定义
c语言·开发语言·算法
编程彩机1 小时前
互联网大厂Java面试:从Java SE到大数据场景的技术深度解析
java·大数据·spring boot·面试·spark·java se·互联网大厂
笨蛋不要掉眼泪2 小时前
Spring Boot集成LangChain4j:与大模型对话的极速入门
java·人工智能·后端·spring·langchain
筵陌2 小时前
算法:模拟
算法
Yvonne爱编码2 小时前
JAVA数据结构 DAY3-List接口
java·开发语言·windows·python
We་ct2 小时前
LeetCode 205. 同构字符串:解题思路+代码优化全解析
前端·算法·leetcode·typescript
renhongxia12 小时前
AI算法实战:逻辑回归在风控场景中的应用
人工智能·深度学习·算法·机器学习·信息可视化·语言模型·逻辑回归