add new solutions
This commit is contained in:
parent
d798d5e8c9
commit
886b5e0a8e
34 changed files with 1164 additions and 0 deletions
30
solutions/0/q39/solution.go
Normal file
30
solutions/0/q39/solution.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package q39
|
||||
|
||||
import "slices"
|
||||
|
||||
func mkCombs(candidates []int, target int, buf []int, ret [][]int) [][]int {
|
||||
if target == 0 {
|
||||
cp := make([]int, len(buf))
|
||||
copy(cp, buf)
|
||||
return append(ret, buf)
|
||||
}
|
||||
if len(candidates) == 0 || candidates[0] > target {
|
||||
return ret
|
||||
}
|
||||
for i := 0; candidates[0]*i <= target; i++ {
|
||||
ret = mkCombs(candidates[1:], target-i*candidates[0], buf, ret)
|
||||
buf = append(buf, candidates[0])
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func combinationSum(candidates []int, target int) [][]int {
|
||||
slices.Sort(candidates)
|
||||
|
||||
ret := make([][]int, 0, 150)
|
||||
buf := make([]int, target/candidates[0]+1)
|
||||
|
||||
return mkCombs(candidates, target, buf[:0], ret)
|
||||
}
|
||||
|
||||
var _ = combinationSum
|
||||
Loading…
Add table
Add a link
Reference in a new issue