1232. Check If It Is a Straight Line

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

求所有点和第一个点的dx和dy,然后判断下dxi * dyj == dyi * dxj,如果打算求k和b的话得注意一下k=0和正无穷

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

 

你可能感兴趣的:(1232. Check If It Is a Straight Line)