-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31.py
More file actions
34 lines (30 loc) · 897 Bytes
/
31.py
File metadata and controls
34 lines (30 loc) · 897 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
# -*- coding: utf-8 -*-
# @Time : 2020/10/12 0012 19:15
# @Author : zhengwei
# @File : 31.py
# @Software: PyCharm
class Solution:
def nextPermutation(self, nums):
"""
Do not return anything, modify nums in-place instead.
"""
pos1 = 0
pos2 = 0
for i in range(-2, -len(nums), -1):
if nums[i-1] < nums[i+1]:
pos1 = i
if pos1 == 0:
return sorted(nums)
else:
for j in range(-1, -len(nums), -1):
if nums[j] > nums[pos1]:
pos2 = j
nums[pos1], nums[pos2] = nums[pos2],nums[pos1]
if len(nums[pos1+1:]) > 1:
result = nums[:pos1+1] + sorted(nums[pos1+1:])
else:
result = nums
return result
test = Solution()
result = test.nextPermutation([3,2,1])
print(result)