-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
26 lines (23 loc) · 838 Bytes
/
Solution.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
from typing import List
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
index = None
for i in range(len(nums) - 1, 0, -1):
if nums[i] > nums[i - 1]:
exchange = len(nums) - 1
while nums[i - 1] >= nums[exchange]:
exchange -= 1
nums[i - 1], nums[exchange] = nums[exchange], nums[i - 1]
index = i - 1
break
if index is None:
nums.reverse()
return nums
for i in range(index + 1, len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] > nums[j]:
nums[i], nums[j] = nums[j], nums[i]
return nums