Question
Given two integers n and k, return all possible combinations of k numbers out of 1 … n.
For example, If n = 4 and k = 2, a solution is:
1 | |
Analysis
典型的回溯问题。解题中遇到的问题对于一个固定的己选值,怎样确定它下面所有可能的解值。比如选定了一个数i, 下面选择的数要从i+1开始。所以对DFS要传进去一个值表示从当前这个值开始做为可能的子解。其它就是典型的回溯了。
Solution
1 | |
1 | |