Skip to content

Commit 310e943

Browse files
committed
combinations_sum_III
1 parent 9fab52c commit 310e943

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
173173
#### [213. House Robber II](https://github.com/hitzzc/go-leetcode/tree/master/add_and_search_word)
174174
#### [214. Shortest Palindrome (unsolved)](https://github.com/hitzzc/go-leetcode/tree/master/shortest_palindrome)
175175
#### [215. Kth Largest Element in an Array](https://github.com/hitzzc/go-leetcode/tree/master/kth_largest_element_in_an_array)
176+
#### [216. Combination Sum III](https://github.com/hitzzc/go-leetcode/tree/master/combinations_sum_III)
176177

177178

178179

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package combinations_sum_III
2+
3+
func combinationSum3(k int, n int) [][]int {
4+
return DFS(0, 1, k, n, []int{})
5+
}
6+
7+
func DFS(level int, start int, k int, n int, path []int) (ret [][]int) {
8+
if level == k && n == 0 {
9+
cp := make([]int, len(path))
10+
copy(cp, path)
11+
ret = append(ret, cp)
12+
return
13+
}
14+
for i := start; i <= n && i <= 9; i++ {
15+
path = append(path, i)
16+
tmpRet := DFS(level+1, i+1, k, n-i, path)
17+
ret = append(ret, tmpRet...)
18+
path = path[:len(path)-1]
19+
}
20+
return
21+
}

0 commit comments

Comments
 (0)