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
| package main
import "fmt"
func main() { n := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} combineDFS(n, 16) }
var res [][]int = make([][]int, 0)
func combineDFS(num []int, target int) (result [][]int) { if len(num) == 0 { return }
dfs(target, 0, []int{}, num) fmt.Println(res) return res }
func dfs(target int, index int, pre []int, num []int) { if target == 0 { pp := make([]int, len(pre)) copy(pp, pre) res = append(res, pp) return }
for i := index; i < len(num); i++ { if num[i] > target { break }
pre = append(pre, num[i]) dfs(target-num[i], i, pre, num) pre = pre[:len(pre)-1] } }
|