-
Notifications
You must be signed in to change notification settings - Fork 0
/
31.py
51 lines (38 loc) · 1 KB
/
31.py
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
49
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
size = len(nums)
if size == 0:
return nums
i = size-1
while i >= 1:
if nums[i] > nums[i-1]:
break
i -= 1
if i - 1 >= 0:
# insert
t = nums[i-1]
j = size-1
while j >= i:
if nums[j] > t:
nums[i-1] = nums[j]
nums[j] = t
break
j -= 1
# reverse
m = i
n = size-1
while m < n:
temp = nums[m]
nums[m] = nums[n]
nums[n] = temp
m += 1
n -= 1
if __name__ == "__main__":
Solution().nextPermutation([1, 3, 2])