Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
Example:
Input: n = 4, k = 2
Output:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
func combine(n int, k int) [][]int {
if n <= 0 || n < k {
return [][]int{}
}
res := [][]int{}
graph := make([]int, n)
for i := range graph {
graph[i] = i + 1
}
dfs(graph, k, 0, []int{}, &res)
return res
}
func dfs(graph []int, target int, index int, path []int, paths *[][]int) {
if len(path) == target {
tmp := make([]int, target)
copy(tmp, path)
*paths = append(*paths, tmp)
return
}
for i, v := range graph {
if i < index {
continue
}
fmt.Println(path, append(path, v))
dfs(graph, target, index+1, append(path, v), paths)
index++
}
}```