Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 14, 2023
1 parent d95a456 commit af6e4bb
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
)

func combinationSum3(k int, n int) [][]int {
result := [][]int{}
t := []int{}
var dfs func(i, s int)
dfs = func(i, s int) {
if s == 0 {
if len(t) == k {
cp := make([]int, len(t))
copy(cp, t)
result = append(result, cp)
}
return
}
if i > 9 || i > s || len(t) >= k {
return
}
t = append(t, i)
dfs(i+1, s-i)
t = t[:len(t)-1]
dfs(i+1, s)
}
dfs(1, n)
return result
}

func main() {
k := 3
n := 7
fmt.Println(combinationSum3(k, n))
}

0 comments on commit af6e4bb

Please sign in to comment.