Topic:
There are some points in an XY coordinate system. We use the array coordinates to record their coordinates respectively. Coordinates[i] = [x, y] represents the point with the abscissa as x and the ordinate as y.
Please judge whether these points are on the same straight line in the coordinate system. If yes, return true, otherwise, return false.
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
Code:
class Solution {
public:
bool checkStraightLine(vector<vector<int>>& coordinates) {
int n = coordinates.size();
for(int i=1;i<coordinates.size()-1;++i){
if((coordinates[i][0] - coordinates[0][0])*(coordinates[i][1] - coordinates[n-1][1])
!=
(coordinates[i][1] - coordinates[0][1])*(coordinates[i][0] - coordinates[n-1][0]))
return false;
}
return true;
}
};