-
Notifications
You must be signed in to change notification settings - Fork 0
/
NextPermutations.py
30 lines (28 loc) · 958 Bytes
/
NextPermutations.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
class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
if nums is None:
return [[]]
elif len(nums) <= 1:
return nums
# Find the first nums[k]<nums[k+1]
for index in range(len(nums) - 2, -1, -1):
if nums[index] < nums[index + 1]:
break
elif index == 0:
return sorted(nums)
#Finde the first one j nums[j]>nums[index]
for j in range(len(nums) - 1, index, -1):
if nums[j] > nums[index]:
break
#Exchange the num[j],nums[index]
nums[j], nums[index] = nums[index], nums[j]
nums[index + 1:] = nums[len(nums) - 1:index:-1]
return nums
if __name__ == '__main__':
nums1 = [1]
index = Solution().nextPermutation(nums1)
print(index)