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
| func permuteUnique(nums []int) [][]int { path, res := make([]int, 0), make([][]int, 0) used := make([]bool, len(nums)) var backtracking func() backtracking = func() { if len(path) == len(nums) { tmp := make([]int, len(path)) copy(tmp, path) res = append(res, tmp) return }
for i := 0; i < len(nums); i++ { if i > 0 && nums[i-1] == nums[i] && !used[i-1] { continue } if !used[i] { used[i] = true path = append(path, nums[i]) backtracking() path = path[:len(path)-1] used[i] = false } } } backtracking() return res }
|