Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c238fc0

Browse files
kant-liazl397985856
authored andcommitted
feat: Add python3 solution for problem 31 (azl397985856#115)
* Fix solution on problem 88 with js, fixs azl397985856#62 * Fix solution on problem 88, fixs azl397985856#62 * Add python3 solution for problem 31
1 parent b5afc27 commit c238fc0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

problems/31.next-permutation.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,37 @@ var nextPermutation = function(nums) {
101101
reverseRange(nums, i + 1, nums.length - 1);
102102
};
103103
```
104+
Python3 Code:
105+
```python
106+
class Solution:
107+
def nextPermutation(self, nums):
108+
"""
109+
Do not return anything, modify nums in-place instead.
110+
:param list nums
111+
"""
112+
# 第一步,从后往前,找到下降点
113+
down_index = None
114+
for i in range(len(nums)-2, -1, -1):
115+
if nums[i] < nums[i+1]:
116+
down_index = i
117+
break
118+
# 如果没有下降点,重新排列
119+
if down_index is None:
120+
nums.reverse()
121+
# 如果有下降点
122+
else:
123+
# 第二步,从后往前,找到比下降点大的数,对换位置
124+
for i in range(len(nums)-1, i, -1):
125+
if nums[down_index] < nums[i]:
126+
nums[down_index], nums[i] = nums[i], nums[down_index]
127+
break
128+
# 第三部,重新排列下降点之后的数
129+
i, j = down_index+1, len(nums)-1
130+
while i < j:
131+
nums[i], nums[j] = nums[j], nums[i]
132+
i += 1
133+
j -= 1
134+
```
104135

105136
## 相关题目
106137

0 commit comments

Comments
 (0)