-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39.go
More file actions
33 lines (27 loc) · 688 Bytes
/
39.go
File metadata and controls
33 lines (27 loc) · 688 Bytes
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
package main
import "sort"
func CombinationSum(candidates []int, target int) [][]int {
result := [][]int{}
sub := []int{}
sort.Ints(candidates)
helper39(candidates, target, &result, sub)
return result
}
func helper39(candidates []int, target int, result *[][]int, sub []int) {
if len(candidates) == 0 {
return
}
if candidates[0] == target {
sub = append(sub, candidates[0])
*result = append(*result, sub)
return
} else if candidates[0] < target {
helper39(candidates[1:], target, result, sub)
sub2 := make([]int, len(sub))
copy(sub2, sub)
sub2 = append(sub2, candidates[0])
helper39(candidates, target-candidates[0], result, sub2)
} else {
return
}
}