LeetCode (1232. dotted into a line)

LeetCode (1232. dotted into a line)


There are some points in an XY coordinate system. We use the array coordinates to record their coordinates respectively, where coordinates[i] = [x, y] represents the point with the abscissa x and the ordinate 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
 

prompt:

2 <= coordinates.length <= 1000
coordinates[i].length == 2
-10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
coordinates 中不含重复的点

Source: LeetCode
Link: https://leetcode-cn.com/problems/check-if-it-is-a-straight-line

answer:

class Solution:
    def checkStraightLine(self, c: List[List[int]]) -> bool:
        (x1, y1), (x2, y2) = c[:2]
        for x,y in c[2:]:
            if (x-x1)*(y1-y2) !=(y-y1) *(x1-x2) :return False
        return True

Guess you like

Origin blog.csdn.net/adminkeys/article/details/112756944