topic
There the XY coordinate system in a number of points, we used the array coordinates
to their coordinates were recorded, where coordinates[i] = [x, y]
abscissa is the x
ordinate of y
the point.
Please judge whether these points are on the same straight line in the coordinate system true
, if yes, return , otherwise, please return false
.
Example 1:
输入:coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
输出:true
Example 2:
输入:coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
输出:false
prompt:
2 <= coordinates.length <= 1000
coordinates[i].length == 2
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
coordinates
Does not contain duplicate points
Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/check-if-it-is-a-straight-line
Copyright is owned by LeetCode . For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
answer
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
for (int i = 2;i < coordinates.length;i ++) {
if ((coordinates[1][1]-coordinates[0][1])*(coordinates[i][0]-coordinates[0][0])!=(coordinates[i][1]-coordinates[0][1])*(coordinates[1][0]-coordinates[0][0])) {
return false;
}
}
return true;
}
}
0ms 38MB
Because of division reasons, it is impossible to have 0, so I found a formula deduced by others on the Internet, fixed two points, and then input the coordinates of the third point to determine whether it is the same straight line
Learning exchange q group: 470464545, everyone study and progress together, CSDN blog: lolly1023