-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationCombination.py
More file actions
66 lines (53 loc) · 1.42 KB
/
PermutationCombination.py
File metadata and controls
66 lines (53 loc) · 1.42 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from collections import Counter
def permuteUnique(nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
permutations = []
counter = Counter(nums)
def findAllPermutations(res):
if len(res) == len(nums): # if meet
permutations.append(res)
return
for key in counter:
if counter[key]:
counter[key] -= 1 # decrement visited key
findAllPermutations(res + [key])
counter[key] += 1 # restore the state of visited key to find the next path
findAllPermutations([])
return permutations
def next_permutation(l):
n = len(l)
if n <= 1:
return False
i = n - 1
while True:
j = i
i -= 1
if l[i] < l[j]:
k = n - 1
while not (l[i] < l[k]):
k -= 1
l[i], l[k] = l[k], l[i]
l[j:] = reversed(l[j:])
return True
if i == 0:
l.reverse()
return False
def permute2(nums):
# time consuming
# nums = [0, 1]
ret = []
def add_perm(lst, cur):
if len(cur) == len(nums):
ret.append(cur)
if lst == []:
return
else:
for i in range(len(lst)):
add_perm(lst[:i] + lst[i + 1:], cur + [lst[i]])
add_perm(nums, [])
return ret
print(permuteUnique([1, 1, 2]))
print(permute2([0, 1, 2]))