Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 2 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Sep 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update 0031.下一个排列.md
完善python代码
  • Loading branch information
casnz1601 authored Sep 6, 2022
commit 3975f442848c6790178fb6950181ff3fe50ee954
77 changes: 19 additions & 58 deletions problems/0031.下一个排列.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -160,73 +160,34 @@ class Solution {
```

## Python
>直接使用sorted()不符合题意
>直接使用sorted()会开辟新的空间并返回一个新的list,故补充一个原地反转函数
```python
class Solution:
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(len(nums)-1, -1, -1):
for j in range(len(nums)-1, i, -1):
length = len(nums)
for i in range(length - 1, -1, -1):
for j in range(length - 1, i, -1):
if nums[j] > nums[i]:
nums[j], nums[i] = nums[i], nums[j]
nums[i+1:len(nums)] = sorted(nums[i+1:len(nums)])
return
nums.sort()
```
>另一种思路
```python
class Solution:
'''
抛砖引玉:因题目要求"必须原地修改,只允许使用额外常数空间",python内置sorted函数以及数组切片+sort()无法使用。
故选择另一种算法暂且提供一种python思路
'''
def nextPermutation(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
length = len(nums)
for i in range(length-1, 0, -1):
if nums[i-1] < nums[i]:
for j in range(length-1, 0, -1):
if nums[j] > nums[i-1]:
nums[i-1], nums[j] = nums[j], nums[i-1]
break
self.reverse(nums, i, length-1)
break
if n == 1:
# 若正常结束循环,则对原数组直接翻转
self.reverse(nums, 0, length-1)

def reverse(self, nums: List[int], low: int, high: int) -> None:
while low < high:
nums[low], nums[high] = nums[high], nums[low]
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]
self.reverse(nums, i + 1, length - 1)
return
self.reverse(nums, 0, length - 1)

start, end = i+1, n-1
while start < end:
nums[start], nums[end] = nums[end], nums[start]
start += 1
end -= 1

return nums
def reverse(self, nums: List[int], left: int, right: int) -> None:
while left < right:
nums[left], nums[right] = nums[right], nums[left]
left += 1
right -= 1

"""
265 / 265 个通过测试用例
状态:通过
执行用时: 36 ms
内存消耗: 14.9 MB
"""
```

## Go
Expand Down

AltStyle によって変換されたページ (->オリジナル) /