-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination_sum_ii.go
37 lines (34 loc) · 1000 Bytes
/
combination_sum_ii.go
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
29
30
31
32
33
34
35
36
37
package backtracking
import "sort"
func CombinationSum2(candidates []int, target int) [][]int {
// List to store final output
var combinations [][]int
// Special case
if len(candidates) == 0 {
return combinations
}
// Sort the array
sort.Ints(candidates)
// Perform backtracking
backtrackForCombinationSumII(candidates, 0, []int{}, &combinations, target)
return combinations
}
func backtrackForCombinationSumII(candidates []int, index int, combination []int, combinations *[][]int, target int) {
if target == 0 {
var temp = make([]int, len(combination))
copy(temp, combination)
*combinations = append(*combinations, temp)
return
}
if target < 0 {
return
}
for i := index; i < len(candidates); i++ {
if i > index && candidates[i] == candidates[i-1] {
continue
}
combination = append(combination, candidates[i])
backtrackForCombinationSumII(candidates, i+1, combination, combinations, target-candidates[i])
combination = combination[:len(combination)-1]
}
}