【JAVA练习】力扣1232.缀点成线

题目:

解题思路:

直线上面的点满足公式:,

如果所有点在一条直线上,即任意两点的斜率相同,由于计算斜率存在精度的问题,可以将除法等价于乘法。

等价于

java 复制代码
class Solution {
    public boolean checkStraightLine(int[][] coordinates) {
        int x = coordinates[0][0] - coordinates[1][0];
        int y = coordinates[0][1] - coordinates[1][1];

        for (int i = 1; i < coordinates.length-1; i++) {
            int new_x = coordinates[i][0] - coordinates[i+1][0];
            int new_y = coordinates[i][1] - coordinates[i+1][1];
            if (new_y * x != new_x * y) {
                return false;
            }
        }
        return true;
    }
}
// ny / nx = y / x
相关推荐
wuminyu4 小时前
JVM规范学习总结:解释器与JIT编译器如何处理JVM字节码
java·linux·c语言·jvm·c++
txxzjmzlh4 小时前
SpringBoot 配置⽂件
java·spring boot·spring
ZhengEnCi4 小时前
模型训练完整流程:从数据准备到模型上线的 10 个关键步骤(全文可跑)
后端·算法
Wang's Blog4 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象3274 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
独泪了无痕5 小时前
Hutool之LocalDateTimeUtil:让时间间隔操作如虎添翼
java
三无少女指南6 小时前
从这里开始:Java 后端与 AI 工程的三条阅读路线
java·学习路线·ai工程
香蕉大香蕉(06敬畏市场)6 小时前
MySQL 单点详解(07)
java·数据库·mysql·ai·博客·八股
nice_lcj5207 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试