LeetCode 31 Next Permutation (Python)

Posted by 小明MaxMing on January 31, 2021

题目

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).

The replacement must be in place and use only constant extra memory.

解题思路

从后向前遍历找到第一个升序的位置,再次从后向前遍历找到第一个比它大的数,进行交换,再对后面的数排序,如果没有升序的位置,将数组返序

代码

class Solution:
    def nextPermutation(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        i = n - 2
        while i >= 0:
            if nums[i] >= nums[i + 1]:
                i -= 1
            else:
                for j in range(n - 1, i, -1):
                    if nums[j] > nums[i]:
                        nums[i], nums[j] = nums[j], nums[i]
                        nums[i + 1:] = sorted(nums[i + 1:])
                        return
        nums.reverse()

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅