LeetCode--Python解析【Maximum Gap】(164)

题目:


方法:

使用了python中的内置排序方法sort()

剩下就是根据题目要求,依次计算差值

最后返回最大差值

class Solution(object):
    def maximumGap(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res = 0
        if len(nums) < 2:
            return 0
        nums.sort()
        for i in range(len(nums)-1):
            x = abs(nums[i] - nums[i+1])
            res = max(x,res)
        return res

猜你喜欢

转载自blog.csdn.net/zjrn1027/article/details/80397072