天天看點

Leetcode刷題記錄——1232. 綴點成線

Leetcode刷題記錄——1232. 綴點成線
Leetcode刷題記錄——1232. 綴點成線

國中數學方法

class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        length = len(coordinates)
        if length <= 2:
            return True
        deltax = coordinates[0][0] - coordinates[1][0]#x0 - x1
        deltay = coordinates[0][1] - coordinates[1][1]#y0 - y1
        k = None if deltax == 0 else (deltay)/(deltax)
        b = None if deltax == 0 else coordinates[0][1]-((deltay/deltax)*coordinates[0][0])
        for i in range(2,length):
            tempdeltax = coordinates[0][0] - coordinates[i][0]
            tempdeltay = coordinates[0][1] - coordinates[i][1]
            tempk = None if tempdeltax == 0 else (tempdeltay)/(tempdeltax)
            tempb = None if tempdeltax == 0 else coordinates[0][1]-((tempdeltay/tempdeltax)*coordinates[0][0])
            if tempk != k or tempb != b:
                return False
        return True