其他分享
首页 > 其他分享> > LeetCode #1502. Can Make Arithmetic Progression From Sequence

LeetCode #1502. Can Make Arithmetic Progression From Sequence

作者:互联网

题目

1502. Can Make Arithmetic Progression From Sequence


解题方法

先排序数组,取前两个数算出差值dif,然后从第二个数开始对比到结尾,如果相邻差值不等于dif就返回False,遍历结束返回True。
时间复杂度:O(n)
空间复杂度:O(1)


代码

class Solution:
    def canMakeArithmeticProgression(self, arr: List[int]) -> bool:
        arr.sort()
        dif = arr[1] - arr[0]
        for i in range(1, len(arr)-1):
            if arr[i+1] - arr[i] != dif:
                return False
        else:
            return True

标签:arr,return,Progression,Sequence,Make,1502,dif,Arithmetic
来源: https://www.cnblogs.com/RatsCommander/p/14073228.html