-
Notifications
You must be signed in to change notification settings - Fork 0
/
q31_Next_Permutation.py
43 lines (40 loc) · 1.13 KB
/
q31_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
34
35
36
37
38
39
40
41
42
43
class Solution:
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i = len(nums) - 1
while i > 0:
if nums[i] > nums[i - 1]:
break
i -= 1
if i == 0:
nums[:] = nums[::-1]
else:
j = i - 1
k = i + 1
while k < len(nums):
if nums[k] < nums[i] and nums[k] > nums[j]:
i = k
k += 1
nums[j], nums[i] = nums[i], nums[j]
nums[j + 1:] = sorted(nums[j + 1:])
def nextPermutation2(self, nums):
i = len(nums) - 1
while i - 1 >= 0 and nums[i - 1] >= nums[i]:
i -= 1
if i == 0:
nums[:] = nums[::-1]
return
else:
j = i
while j + 1 < len(nums) and nums[j + 1] > nums[i - 1]:
j += 1
nums[i - 1], nums[j] = nums[j], nums[i - 1]
nums[i:] = nums[i:][::-1]
return
s = Solution()
num = [2, 4, 1]
s.nextPermutation(num)
print(num)