Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1598341

Browse files
authored
Update 0031.下一个排列.md
补充python sorted()代码
1 parent 75e1bb2 commit 1598341

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

problems/0031.下一个排列.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,22 @@ class Solution {
120120
```
121121

122122
## Python
123-
123+
>直接使用sorted()不符合题意
124+
```python
125+
class Solution:
126+
def nextPermutation(self, nums: List[int]) -> None:
127+
"""
128+
Do not return anything, modify nums in-place instead.
129+
"""
130+
for i in range(len(nums)-1, -1, -1):
131+
for j in range(len(nums)-1, i, -1):
132+
if nums[j] > nums[i]:
133+
nums[j], nums[i] = nums[i], nums[j]
134+
nums[i+1:len(nums)] = sorted(nums[i+1:len(nums)])
135+
return
136+
nums.sort()
137+
```
138+
>另一种思路
124139
```python
125140
class Solution:
126141
'''

0 commit comments

Comments
 (0)