-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutation II.py
More file actions
29 lines (20 loc) · 796 Bytes
/
Copy pathPermutation II.py
File metadata and controls
29 lines (20 loc) · 796 Bytes
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
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
res = []
perm = []
count = { n:0 for n in nums }
for n in nums:
count[n] += 1
def dfs():
if len(perm) == len(nums):
res.append(perm.copy()) #we are making a copy as one variable and perm will be updated everytime
return
for n in count:
if count[n] > 0:
perm.append(n)
count[n] -= 1
dfs()
count[n] += 1
perm.pop()
dfs()
return res