码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode-159周赛-5230-缀点成线

时间:2019-10-21 09:40:44      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:lse   pre   http   else   img   def   str   for   ima   

技术图片

 

 

自己的提交:

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

 

leetcode-159周赛-5230-缀点成线

标签:lse   pre   http   else   img   def   str   for   ima   

原文地址:https://www.cnblogs.com/oldby/p/11711521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!