-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path47_PermutationsII.py
executable file
·65 lines (53 loc) · 1.65 KB
/
47_PermutationsII.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env python
# -*- coding: utf-8 -*-
class Solution(object):
# Easy to understand: recursively.
# Just like get permute for distinct numbers.
def permuteUnique(self, nums):
ans = []
nums.sort()
self.dfs(nums, 0, ans)
return ans
def dfs(self, num, begin, ans):
if begin == len(num) - 1:
ans.append(num)
return
for i in range(begin, len(num)):
if i != begin and num[i] == num[begin]:
continue
num[i], num[begin] = num[begin], num[i]
# num[:], get a new copy. Just like pass by value
self.dfs(num[:], begin + 1, ans)
class Solution_2(object):
'''
1. sort nums in ascending order, add it to res;
2. generate the next permutation of nums, and add it to res;
3. repeat 2 until the next permutation of nums.
'''
def permuteUnique(self, nums):
nums.sort()
ans = []
ans.append(nums[:])
while self.nextPermutation(nums):
ans.append(nums[:])
return ans
def nextPermutation(self, nums):
length = len(nums)
index = length - 1
while index >= 1:
if nums[index] > nums[index - 1]:
for i in range(length - 1, index - 1, -1):
if nums[i] > nums[index - 1]:
nums[i], nums[index - 1] = nums[index - 1], nums[i]
nums[index:] = sorted(nums[index:])
return True
else:
index -= 1
# Nums is in descending order, just reverse it.
return False
"""
[]
[1]
[1,2,3]
[2,2,3,3]
"""