Skip to content

Commit d383df0

Browse files
authored
Merge pull request neetcode-gh#2065 from josuebrunel/feat/0047-permutations-ii.go
Create 0047-permutations-ii.go
2 parents fef3c9c + 6c137fe commit d383df0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

go/0047-permutations-ii.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
func permuteUnique(nums []int) [][]int {
2+
numLen := len(nums)
3+
res := [][]int{}
4+
counter := make(map[int]int)
5+
6+
for _, n := range nums {
7+
counter[n]++
8+
}
9+
10+
var backtrack func([]int, map[int]int)
11+
12+
backtrack = func(perm []int, counter map[int]int) {
13+
if len(perm) == numLen {
14+
res = append(res, append([]int{}, perm...))
15+
return
16+
}
17+
18+
for n, count := range counter {
19+
if count == 0 {
20+
continue
21+
}
22+
perm = append(perm, n)
23+
counter[n]--
24+
backtrack(perm, counter)
25+
perm = perm[:len(perm)-1]
26+
counter[n]++
27+
}
28+
}
29+
30+
backtrack([]int{}, counter)
31+
32+
return res
33+
}

0 commit comments

Comments
 (0)