1232. 缀点成线

在一个 XY 坐标系中有一些点,我们用数组 coordinates 来分别记录它们的坐标,其中 coordinates[i] = [x, y] 表示横坐标为 x、纵坐标为 y 的点。

请你来判断,这些点是否在该坐标系中属于同一条直线上,是则返回 true,否则请返回 false

 

示例 1:

输入:coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]
输出:true

示例 2:

输入:coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]
输出:false

 

提示:

  • 2 <= coordinates.length <= 1000
  • coordinates[i].length == 2
  • -10^4 <= coordinates[i][0], coordinates[i][1] <= 10^4
  • coordinates 中不含重复的点

如果用除法算斜率,要考虑斜率不存在的情况

class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        if len(coordinates)<=2:return True
        if len(set(list(x[0] for x in coordinates)))<len(coordinates):
            if len(set(list(x[0] for x in coordinates)))==1:
                return True
            else:
                return False
        for i in range(1,len(coordinates)):
            if (coordinates[i][1]-coordinates[0][1])/(coordinates[i][0]-coordinates[0][0])!=(coordinates[1][1]-coordinates[0][1])/(coordinates[1][0]-coordinates[0][0]):
                return False
        return True

 

还可以用乘法,就不用考虑

class Solution:
    def checkStraightLine(self, coordinates: List[List[int]]) -> bool:
        if len(coordinates) <= 2:
            return True

        dy = coordinates[1][1] - coordinates[0][1]
        dx = coordinates[1][0] - coordinates[0][0]

        for i in range(2, len(coordinates)):
            dy1 = coordinates[i][1] - coordinates[0][1]
            dx1 = coordinates[i][0] - coordinates[0][0]
            if dy * dx1 != dy1 * dx:
                return False
        
        return True

 

posted @ 2021-01-17 14:58  XXXSANS  阅读(105)  评论(0编辑  收藏  举报