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

题目:


方法:

python可以用一种偷懒的方法

使用python内置的sort()排序

直接一行代码搞定

class Solution(object):
    def sortColors(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        nums.sort()

我们再用传统的冒泡排序完成一遍

class Solution(object):
    def sortColors(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        for i in range(len(nums)-1):
            for j in range(i+1,len(nums)):
                if nums[i] > nums[j] :
                    temp = nums[i]
                    nums[i] = nums[j]
                    nums[j] = temp
很简单的一道题,考察排序的方法。

猜你喜欢

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