-
Notifications
You must be signed in to change notification settings - Fork 19
/
answer.py
42 lines (33 loc) · 1.1 KB
/
answer.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
#!/usr/bin/python3
#------------------------------------------------------------------------------
from collections import defaultdict
from copy import deepcopy
class Solution:
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
freq_list = self.freq(nums)
return self.backtrack([], nums, [], freq_list, len(nums))
# Build freq list
def freq(self, nums):
freq_list = defaultdict(int)
for n in nums:
freq_list[n] += 1
return freq_list
def backtrack(self, result, nums, prefix, freq, remaining):
if remaining == 0:
result.append(deepcopy(prefix))
return result
# Handles dups because its a dict
for n in freq:
if freq[n] > 0:
# Backtracking
freq[n] -= 1
self.backtrack(result, nums, prefix+[n], freq, remaining-1)
freq[n] += 1
return result
#------------------------------------------------------------------------------
#Testing