python - next perm


  • 0
    S
    class Solution(object):                
        def nextPermutation(self, nums):
            """
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            
            nums[:] = nums[::-1]
                
            for i in range(len(nums)):
                for j in range(i):
                    if nums[j] > nums[i]:
                        nums[i], nums[j] = nums[j], nums[i]
                        nums[:] = nums[i:][::-1] + sorted(nums[:i])
                        return
            
            nums.sort()
            return
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.