-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmed47.py
34 lines (23 loc) · 1.08 KB
/
med47.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
# given a collection of numbers, nums, that might contain duplicates,
# return all possible unique permutations in any order
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
results = []
def backtrack(curCombo, counter):
# if we're done, add to results
if len(curCombo) == len(nums):
results.append(list(curCombo))
return
# otherwise find the next digit to add to curCombo
for num in counter:
if counter[num] > 0:
# add valid candidate num
curCombo.append(num)
counter[num] -= 1
# countinue building curCombo
backtrack(curCombo, counter)
# backtrack one step
curCombo.pop()
counter[num] += 1
backtrack([], Counter(nums))
return results