forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0031.py
More file actions
29 lines (23 loc) · 691 Bytes
/
0031.py
File metadata and controls
29 lines (23 loc) · 691 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
class Solution:
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i = j = r = len(nums) - 1
while i > 0 and nums[i] <= nums[i-1]:
i -= 1
i -= 1
if i >= 0:
while j >= i and nums[j] <= nums[i]:
j -= 1
nums[i], nums[j] = nums[j], nums[i]
i += 1
while i < r:
nums[i], nums[r] = nums[r], nums[i]
i += 1
r -= 1
if __name__ == "__main__":
nums = [3,2,1]
Solution().nextPermutation(nums)
print(nums)