-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinations.go
More file actions
38 lines (36 loc) · 801 Bytes
/
Copy pathcombinations.go
File metadata and controls
38 lines (36 loc) · 801 Bytes
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
package main
func combine(n int, k int) [][]int {
ans := make([][]int, 0)
option := make([]int, k)
var dfs func(start int, idx int)
dfs = func(start int, idx int) {
for i := start; i <= n-(k-1-idx); i++ {
option[idx] = i
if idx == k-1 {
ans = append(ans, append([]int{}, option...))
} else {
dfs(i+1, idx+1)
}
}
}
dfs(1, 0)
return ans
}
func combine0(n int, k int) [][]int {
ans := make([][]int, 0)
var dfs func(start int, option []int)
dfs = func(start int, option []int) {
for i := start; i <= n+len(option)+1-k; i++ {
option := append(option, i)
if len(option) == k {
ans = append(ans, append([]int{}, option...))
} else {
dfs(i+1, option)
}
option = option[:len(option)-1]
}
}
option := make([]int, 0)
dfs(1, option)
return ans
}