Skip to content

Commit fef3c9c

Browse files
authored
Merge pull request #2064 from josuebrunel/feat/0047-permutations-ii.py
Create 0047-permutations-ii.py
2 parents bdc70c6 + 1cdac98 commit fef3c9c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Diff for: python/0047-permutations-ii.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import collections
2+
3+
4+
class Solution:
5+
6+
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
7+
result = []
8+
counter = collections.Counter(nums)
9+
10+
def backtrack(perm, counter):
11+
if len(perm) == len(nums):
12+
result.append(perm.copy())
13+
14+
for n in counter:
15+
if counter[n] == 0:
16+
continue
17+
perm.append(n)
18+
counter[n] -= 1
19+
backtrack(perm, counter)
20+
perm.pop()
21+
counter[n] += 1
22+
23+
backtrack([], counter)
24+
25+
return result

0 commit comments

Comments
 (0)