leetcode第31题下一个排列

下一个排列的顺序实际就是排序好的深度优先遍历

class Solution(object):
    def nextPermutation(self, nums):
        """
        """
        left = 0
        right = 0
        middle = 0
        for i in range(len(nums)-1)[::-1]:
            if nums[i]>=nums[i+1]:
                pass
            else :
                left = i
                middle = i + 1
                break
        for m in range(middle, len(nums))[::-1]:
            if nums[m]>nums[left]:
                right=m
                break
            else:
                pass
        temp=nums[left]
        nums[left]=nums[right]
        nums[right]=temp
        for n in range(middle,(middle+len(nums))/2):
            temps=nums[n]
            k=middle + len(nums) - 1 - n
            nums[n]=nums[k]
            nums[k]=temps

上一篇:NGINX安装


下一篇:LeetCode简单题之买卖股票的最‭佳时机