一,Combinations
题目描述
Given two integers n and k, return all possible combinations ofk numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
我的代码
class Solution {
public:
vector<int> temp;
vector<vector<int> > res;
public:
void dfs(int index, int depth, int n, int k){
if(depth == k){
res.push_back(temp);
return;
}
if(index > n){
return;
}
for(int i = index; i <= n; i ++){
temp.push_back(i);
dfs(i + 1, depth + 1, n, k);
temp.pop_back();
}
}
vector<vector<int> > combine(int n, int k) {
dfs(1, 0, n, k);
return res;
}
};