-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
0031-next-permutation.py
33 lines (26 loc) · 1.05 KB
/
0031-next-permutation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from itertools import permutations
from typing import List
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
if nums == sorted(nums, key=lambda x: -x):
nums.sort()
return
for i in range(len(nums) - 1, 0, -1):
if nums[i - 1] < nums[i]:
minIdx, minVal = len(nums), float('inf')
for j in range(len(nums) - 1, i - 1, -1):
if nums[j] > nums[i - 1] and nums[j] < minVal:
minVal = nums[j]
minIdx = j
nums[i - 1], nums[minIdx] = nums[minIdx], nums[i - 1]
while True:
swapped = False
for k in range(i, len(nums) - 1):
if nums[k] > nums[k + 1]:
swapped = True
nums[k], nums[k + 1] = nums[k + 1], nums[k]
if swapped == False:
break
return
nums = [1, 2, 3]
print(Solution().nextPermutation(nums))