문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
[x, y]의 좌표를 나타내는 coordinates[i] = [x, y]인 정수 배열 coordinates가 주어진다. 점들이 XY평면에서 직선을 만드는지 확인해라.
#1
Input: coordinates = [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7]]
Output: true
#2
Input: coordinates = [[1, 1], [2, 2], [3, 4], [4, 5], [5, 6], [7, 7]]
Output: false
class Solution {
public boolean checkStraightLine(int[][] coordinates) {
int x1 = coordinates[0][0];
int y1 = coordinates[0][1];
int x2 = coordinates[1][0];
int y2 = coordinates[1][1];
for(int i = 2; i < coordinates.length; i++){
int x = coordinates[i][0];
int y = coordinates[i][1];
if((x - x1) * (y2 - y1) != (y - y1) * (x2 - x1)){
return false;
}
}
return true;
}
}