原题如下
https://leetcode-cn.com/problems/check-if-it-is-a-straight-line/
题解
对于一条直线上的三个点a(x1,y1)、b(x2,y2)、c(x3,y3)来说一般有:(y3-y1)(x2-x1)=(y2-y1)(x2-x1)。
Java代码:
/*
*@v7fgg
*执行用时 :0 ms, 在所有 Java 提交中击败了100.00%的用户
*内存消耗 :39.7 MB, 在所有 Java 提交中击败了87.50%的用户
*2020年6月16日 20:34
*/
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
int dx=coordinates[1][0]-coordinates[0][0];
int dy=coordinates[1][1]-coordinates[0][1];
for(int i=2;i<coordinates.length;i++){
if(dy*(coordinates[i][0]-coordinates[0][0])!=dx*(coordinates[i][1]-coordinates[0][1])){
return false;
}
}
return true;
}
}