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
| func combine(n int, k int) [][]int { curr := []int{} result := [][]int{}
var dfs func(int) dfs = func(i int) { if len(curr) + n - i + 1 < k { return }
if len(curr) == k { result = append(result, append([]int{}, curr...)) return }
curr = append(curr, i) dfs(i + 1)
curr = curr[:len(curr) - 1] dfs(i + 1) }
dfs(1) return result }
|