-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path31_NextPermutation.py
More file actions
48 lines (38 loc) · 1.37 KB
/
Copy path31_NextPermutation.py
File metadata and controls
48 lines (38 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# coding: utf8
"""
题目链接: https://leetcode.com/problems/next-permutation/description.
题目描述:
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending
order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand
column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
"""
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
ll = len(nums)
target = ll - 1
lex = True
for i in range(ll - 2, -1, -1):
if nums[target] <= nums[i]:
target -= 1
else:
target = i
lex = False
break
if not lex:
for i in range(ll - 1, target, -1):
if nums[target] < nums[i]:
nums[target], nums[i] = nums[i], nums[target]
nums[target + 1:] = sorted(nums[target + 1:])
break
else:
nums.reverse()