题意:列出组合。
思路:DFS。
class Solution {
public:
vector<vector<int> > re;
int n, k;
vector<vector<int>> combine(int n, int k) {
this->n = n;
this->k = k;
vector<int> temp;
dfs(temp, 1, k);
return re;
}
int dfs(vector<int> temp, int s, int depth) {
if(depth == 0) {
re.push_back(temp);
return 0;
}
vector<int> temp2;
for(int i = s; i <= this->n ; ++ i) {
temp2 = temp;
temp2.push_back(i);
dfs(temp2, i + 1, depth - 1);
}
return 0;
}
};
476

被折叠的 条评论
为什么被折叠?



