[LeetCode] Check If It is a Straight Line

아르당·2026년 4월 12일

LeetCode

목록 보기
256/303
post-thumbnail

문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음

Problem

[x, y]의 좌표를 나타내는 coordinates[i] = [x, y]인 정수 배열 coordinates가 주어진다. 점들이 XY평면에서 직선을 만드는지 확인해라.

Example

#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

Constraints

  • 2 <= coordinates.length <= 1000
  • coordinates[i].length == 2
  • -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
  • coordinates는 중복된 점을 포함하지 않는다.

Solved

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;
    }
}
profile
내 마음대로 코드 작성하는 세상

0개의 댓글