# 1. From right to left, find the first digit (PartitionNumber) which violate the increase trend.# 2. From right to left, find the first digit which larger than PartitionNumber, call it ChangeNumber.# 3. Swap the PartitionNumber and ChangeNumber.# 4. Reverse all the digit on the right of partition index.classSolution(object):defnextPermutation(self,nums):""" :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """foriinrange(len(nums)-2,-1,-1):ifnums[i]<nums[i+1]:forjinrange(len(nums)-1,i,-1):ifnums[j]>nums[i]:nums[i],nums[j]=nums[j],nums[i]break# can't use nums[i+1:].reverse() to swapforjinrange(0,(len(nums)-i)//2):nums[0+j+(i+1)],nums[len(nums)-1-(i+1)-j+(i+1)]=nums[len(nums)-1-(i+1)-j+(i+1)],nums[0+j+(i+1)]breakelse:nums.reverse()if__name__=="__main__":answer=Solution()printanswer.nextPermutation([1,3,2])