From 322308fbd0c320344af2091778466ec19ccbb042 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Thu, 17 Feb 2022 13:29:03 +0800 Subject: [PATCH] add: 220217 [golang] --- 2202/220217.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2202/220217.go diff --git a/2202/220217.go b/2202/220217.go new file mode 100644 index 0000000..8e8c62b --- /dev/null +++ b/2202/220217.go @@ -0,0 +1,34 @@ +package main + +import "fmt" + +func solver(candidates *[]int, target, prev int) [][]int { + ret := make([][]int, 0) + for _, v := range *candidates { + if v >= prev && (target-v) >= v { + t := solver(candidates, target-v, v) + for i, _ := range t { + t[i] = append([]int{v}, t[i]...) + } + ret = append(ret, t...) + } else if target == v { + ret = append(ret, []int{v}) + } + } + return ret +} + +func combinationSum(candidates []int, target int) [][]int { + return solver(&candidates, target, 0) +} + +func main() { + candidate := []int{2, 3, 5} + ret := combinationSum(candidate, 8) + for _, v := range ret { + for _, vv := range v { + fmt.Printf("%d ", vv) + } + fmt.Println() + } +}