Problem
Find the next Permutation.
Algorithm
Divide the string into two parts at the first increasing position from the end direction. Then find the new head of the end part and sort the other number in incresing order.
Code
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
sNums = len(nums)
index = sNums-1
while index:
if nums[index] > nums[index-1]:
break
index -= 1
if index:
index2 = index
for i in range(index+1, sNums):
if nums[i] > nums[index-1] and nums[i] < nums[index2]:
index2 = i
buffer = nums[index-1]
nums[index-1] = nums[index2]
nums[index2] = buffer
nums[index:sNums] = sorted(nums[index:sNums])