LeetCode 1232 Check If It Is a Straight Line (Python)

Posted by 小明MaxMing on May 8, 2020

题目

You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.

解题思路

取前两点,求斜率,然后用后面的每个点和第一个点求斜率,如果所有的斜率都相同,则所有点共线,并不需要把斜率求出来,只需要记录y轴和x轴的差,相乘就可以了

代码

class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        dy = coordinates[1][1] - coordinates[0][1]
        dx = coordinates[1][0] - coordinates[0][0]
        for i in range(2, len(coordinates)):
            dyp = coordinates[i][1] - coordinates[0][1]
            dxp = coordinates[i][0] - coordinates[0][0]
            if dy * dxp != dx * dyp:
                return False
        return True

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅