31. Next Permutation
Medium
😇 Solution
class Solution:
def permutation(lst,x):
res = []
if len(lst) < 1:
res.append(x)
return res
for l in lst:
for i in range(len(l)+1):
temp = l[:i] + x + l[i:]
if temp not in res:
res.append(temp)
return res
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
st = ''
for n in nums:
st += str(n)
lst = []
for s in st:
lst = Solution.permutation(lst,s)
lst.sort()
i = 0
while(i <= len(lst)-2):
if lst[i] == st:
nums[:] = list(lst[i+1])
break
i += 1
if i == len(lst)-1:
nums[:] = list(lst[0]) Last updated