Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 806 Bytes

1232.Check-If-it-is-a-Straight-Line.md

File metadata and controls

31 lines (23 loc) · 806 Bytes

1232. Check If It Is a Straight Line

Difficulty: Easy

URL

https://leetcode.com/problems/check-if-it-is-a-straight-line/

Solution

Approach 1:

The code is shown below:

class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        coordinates.sort(key = lambda a : a[0])
        x_len = coordinates[1][0] - coordinates[0][0]
        y_len = coordinates[1][1] - coordinates[0][1]
        cur_x = coordinates[0][0]
        cur_y = coordinates[0][1]
        for i in range(1, len(coordinates)):
            next_x = coordinates[i][0]
            next_y = coordinates[i][1]
            if (next_y - cur_y) * x_len != y_len * (next_x - cur_x):
                return False
            cur_x, cur_y = next_x, next_y
        return True