LeetCode 77. Combinations
Solution1:我的答案
DFS,时间复杂度O(n!)O(n!),空间复杂度O(n)O(n)
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int> > res;
vector<int> temp;
my_combine(n, res, temp, 0, k);
return res;
}
void my_combine(int n,
vector<vector<int> >& res,
vector<int>& temp,
int start, int k) {
if (temp.size() == k) {
res.push_back(temp);
return;
} else {
for (int i = start; i < n; i++) {
temp.push_back(i+1);
my_combine(n, res, temp, i + 1, k);
temp.pop_back();
}
}
}
};