Home Check If It Is a Straight Line
Post
Cancel

Check If It Is a Straight Line

Leetcode Problem

Check If It Is a Straight Line

주어진 점들이 일직선을 이루는 지 판별하는 문제입니다.

1
2
3
4
5
6
7
class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        x, y = coordinates[1][0] - coordinates[0][0], coordinates[1][1] - coordinates[0][1]
        for i in range(2, len(coordinates)):
            if x * (coordinates[i][1] - coordinates[0][1]) != y * (coordinates[i][0] - coordinates[0][0]):
                return False
        return True

첫번째 점과 두번째 점으로 기울기를 구한 다음, 첫번째 점과 세번째 점부터의 기울기가 일치하는 지를 비교합니다.





참고

This post is licensed under CC BY 4.0 by the author.