-
Notifications
You must be signed in to change notification settings - Fork 4
/
solution.go
42 lines (35 loc) · 883 Bytes
/
solution.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
38
39
40
41
42
package main
import (
"fmt"
"sort"
)
func combinationSum2(candidates []int, target int) [][]int {
sort.Ints(candidates)
var result [][]int
var current []int
findCombination(candidates, 0, target, current, &result)
return result
}
func findCombination(candidates []int, index int, target int, current []int, result *[][]int) {
if target == 0 {
temp := make([]int, len(current))
copy(temp, current)
*result = append(*result, temp)
return
}
if target < 0 {
return
}
for i := index; i < len(candidates); i++ {
if i == index || candidates[i] != candidates[i-1] {
current = append(current, candidates[i])
findCombination(candidates, i+1, target-candidates[i], current, result)
current = current[:len(current)-1]
}
}
}
func main() {
candidates := []int{10, 1, 2, 7, 6, 1, 5}
target := 8
fmt.Printf("%#v", combinationSum2(candidates, target))
}