1232.缀点成线
链接:1232.缀点成线
难度:Easy
标签:几何、数组、数学
简介:在一个 XY 坐标系中有一些点,我们用数组 coordinates 来分别记录它们的坐标,其中 coordinates[i] = [x, y] 表示横坐标为 x、纵坐标为 y 的点。请你来判断,这些点是否在该坐标系中属于同一条直线上,是则返回 true,否则请返回 false。
题解 1 - typescript
- 编辑时间:2021-01-17
- 执行用时:92ms
- 内存消耗:40.4MB
- 编程语言:typescript
- 解法介绍:计算直线方程式。
function checkStraightLine(coordinates: number[][]): boolean {
const len = coordinates.length;
if (len <= 2) return true;
const [[x1, y1], [x2, y2]] = coordinates;
if (x1 === x2) return coordinates.every(([x]) => x === x1);
const k = (y1 - y2) / (x1 - x2);
const b = y1 - k * x1;
return coordinates.every(([x, y]) => y === k * x + b);
}