-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
37 lines (27 loc) · 928 Bytes
/
Copy pathSolution.py
File metadata and controls
37 lines (27 loc) · 928 Bytes
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
from _ast import List
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if nums is not None and len(nums) > 1:
length = len(nums)
index = length - 1
while index > 0 and nums[index] <= nums[index - 1]:
index -= 1
index -= 1
if index != -1:
j = length - 1
while j > index and nums[j] <= nums[index]:
j -= 1
nums[j], nums[index] = nums[index], nums[j]
self.reverse(nums, index + 1, length - 1)
def reverse(self, nums: List[int], start: int, end: int) -> None:
if start >= end:
return
i = start
j = end
while j > i:
nums[j], nums[i] = nums[i], nums[j]
j -= 1
i += 1