题目:
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
You may return the answer in any order.
Example 1:
Input: n = 4, k = 2 Output: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
Example 2:
Input: n = 1, k = 1 Output: [[1]]
Constraints:
1 <= n <= 20
1 <= k <= n
又是514作业遇到的……这次真是算法+ood大杂烩了。handout里面给出了一种用bit operation来做的方法,非常直观,所以就直接用了。
方法一:bit operation
我们可以遍历从0到2^n(就是n位一共可以代表的数字个数),如果当前这个数字里面正好有k个bit为1,那么就挑这k个位置来作为一个possible output(当然这里的位置不能从0开始算要从1开始算)。具体怎么得出这些b