Merge pull request #903 from Tian-Jiang/patch-1

Update 0031.下一个排列.md
This commit is contained in:
程序员Carl
2021-11-18 09:50:45 +08:00
committed by GitHub

View File

@ -162,6 +162,29 @@ class Solution:
low += 1
high -= 1
```
>上一版本简化版
'''python
class Solution(object):
def nextPermutation(self, nums: List[int]) -> None:
n = len(nums)
i = n-2
while i >= 0 and nums[i] >= nums[i+1]:
i -= 1
if i > -1: // i==-1,不存在下一个更大的排列
j = n-1
while j >= 0 and nums[j] <= nums[i]:
j -= 1
nums[i], nums[j] = nums[j], nums[i]
start, end = i+1, n-1
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1
return nums
'''
## Go