Python刷力扣 1232. 缀点成线
题目链接1232.缀点成线
思路:判断斜率
y2-y1/x2-x1 == y3-y2/x3-x2
实现
主要是由于初学Python,一开始没理解二维数组如何表示[x,y],其实是这样的形式[[x1][y1],[x2][y2],[x3][y3]…].就可以理解代码中:
x1,y1 = coordinates[0][0] , coordinates[0][1]
代码
class Solution:
def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
x1,y1 = coordinates[0][0],coordinates[0][1]
x2,y2 = coordinates[1][0],coordinates[1][1]
for i in range(len(coordinates)):
x3,y3 = coordinates[i][0],coordinates[i][1]
if (y3-y2)*(x2-x1)!=(y2-y1)*(x3-x2):
return False
return True