Skip to content

Commit 912e274

Browse files
authored
Update 0031.下一个排列.md
1 parent c6fadbd commit 912e274

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

problems/0031.下一个排列.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,29 @@ class Solution:
165165
low += 1
166166
high -= 1
167167
```
168+
>上一版本简化版
169+
'''python
170+
class Solution(object):
171+
def nextPermutation(self, nums: List[int]) -> None:
172+
n = len(nums)
173+
i = n-2
174+
while i >= 0 and nums[i] >= nums[i+1]:
175+
i -= 1
176+
177+
if i > -1: // i==-1,不存在下一个更大的排列
178+
j = n-1
179+
while j >= 0 and nums[j] <= nums[i]:
180+
j -= 1
181+
nums[i], nums[j] = nums[j], nums[i]
182+
183+
start, end = i+1, n-1
184+
while start < end:
185+
nums[start], nums[end] = nums[end], nums[start]
186+
start += 1
187+
end -= 1
188+
189+
return nums
190+
'''
168191

169192
## Go
170193

0 commit comments

Comments
 (0)