We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents fef3c9c + 6c137fe commit d383df0Copy full SHA for d383df0
go/0047-permutations-ii.go
@@ -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
27
28
29
30
+ backtrack([]int{}, counter)
31
32
+ return res
33
+}
0 commit comments