MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / permuteUnique

Function permuteUnique

go/0047-permutations-ii.go:1–33  ·  view source on GitHub ↗
(nums []int)

Source from the content-addressed store, hash-verified

1func 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}

Callers

nothing calls this directly

Calls 1

backtrackFunction · 0.70

Tested by

no test coverage detected