1232 Check If It Is a Straight Line 缀点成线
Description:
You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.
Example:
Example 1:
Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
Output: true
Example 2:
Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
Output: false
Constraints:
2 <= coordinates.length <= 1000
coordinates[i].length == 2
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
coordinates contains no duplicate point.
题目描述:
在一个 XY 坐标系中有一些点,我们用数组 coordinates 来分别记录它们的坐标,其中 coordinates[i] = [x, y] 表示横坐标为 x、纵坐标为 y 的点。
请你来判断,这些点是否在该坐标系中属于同一条直线上,是则返回 true,否则请返回 false。
示例 :
示例 1:
输入:coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
输出:true
示例 2:
输入:coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
输出:false
提示:
2 <= coordinates.length <= 1000
coordinates[i].length == 2
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
coordinates 中不含重复的点
思路:
参考LeetCode #1037 Valid Boomerang 有效的回旋镖
时间复杂度O(n), 空间复杂度O(1)
代码:
C++:
class Solution
{
public:
bool checkStraightLine(vector<vector<int>>& coordinates)
{
int x = coordinates[1][0] - coordinates[0][0], y = coordinates[1][1] - coordinates[0][1];
for (int i = 1; i < coordinates.size(); i++) if ((coordinates[i][0] - coordinates[0][0]) * y != x * (coordinates[i][1] - coordinates[0][1])) return false;
return true;
}
};
Java:
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
int x = coordinates[1][0] - coordinates[0][0], y = coordinates[1][1] - coordinates[0][1];
for (int i = 1; i < coordinates.length; i++) if ((coordinates[i][0] - coordinates[0][0]) * y != x * (coordinates[i][1] - coordinates[0][1])) return false;
return true;
}
}
Python:
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
return all((coordinates[1][1] - coordinates[0][1]) * (coordinates[i][0] - coordinates[0][0]) == (coordinates[i][1] - coordinates[0][1]) * (coordinates[1][0] - coordinates[0][0]) for i in range(2, len(coordinates)))