Skip to content

Commit

Permalink
feat: Add python3 solution for problem 31 (azl397985856#115)
Browse files Browse the repository at this point in the history
* Fix solution on problem 88 with js, fixs azl397985856#62

* Fix solution on problem 88, fixs azl397985856#62

* Add python3 solution for problem 31
  • Loading branch information
kant-li authored and azl397985856 committed Aug 15, 2019
1 parent b5afc27 commit c238fc0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions problems/31.next-permutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,37 @@ var nextPermutation = function(nums) {
reverseRange(nums, i + 1, nums.length - 1);
};
```
Python3 Code:
```python
class Solution:
def nextPermutation(self, nums):
"""
Do not return anything, modify nums in-place instead.
:param list nums
"""
# 第一步,从后往前,找到下降点
down_index = None
for i in range(len(nums)-2, -1, -1):
if nums[i] < nums[i+1]:
down_index = i
break
# 如果没有下降点,重新排列
if down_index is None:
nums.reverse()
# 如果有下降点
else:
# 第二步,从后往前,找到比下降点大的数,对换位置
for i in range(len(nums)-1, i, -1):
if nums[down_index] < nums[i]:
nums[down_index], nums[i] = nums[i], nums[down_index]
break
# 第三部,重新排列下降点之后的数
i, j = down_index+1, len(nums)-1
while i < j:
nums[i], nums[j] = nums[j], nums[i]
i += 1
j -= 1
```

## 相关题目

Expand Down

0 comments on commit c238fc0

Please sign in to comment.