自己的提交:
class Solution: def checkStraightLine(self, coordinates: List[List[int]]) -> bool: if not coordinates: return False if len(coordinates) <= 2: return True k = float("inf") if coordinates[1][0] - coordinates[0][0] == 0 else (coordinates[1][1] - coordinates[0][1]) / (coordinates[1][0] - coordinates[0][0]) for i in range(2,len(coordinates)): k1 = float("inf") if coordinates[i][0] - coordinates[i-1][0] == 0 else (coordinates[i][1] - coordinates[i-1][1]) / (coordinates[i][0] - coordinates[i-1][0]) if k1 != k: return False return True
另:
class Solution: def checkStraightLine(self, coordinates: List[List[int]]) -> bool: n = len(coordinates) if n <= 2: return True x1, y1 = coordinates[0][0] - coordinates[1][0], coordinates[0][1] - coordinates[1][1] for i in range(2, n): x2, y2 = coordinates[0][0] - coordinates[i][0], coordinates[0][1] - coordinates[i][1] if x1 * y2 - x2 * y1 != 0: return False return True
原文地址:https://www.cnblogs.com/oldby/p/11711521.html
时间: 2024-11-07 15:06:23