代码随想录算法训练营第二十四天| 77. 组合
77. 组合
class Solution {
public:
vector<vector<int>> ans;
vector<int> group;
void backtracking(int begin, const int& n, const int& k) {
if (group.size() == k) {
ans.push_back(group);
return;
}
int end = n - (k - group.size()) + 1;
for (int i = begin; i <= end; i++) {
group.push_back(i);
backtracking(i + 1, n, k);
group.pop_back();
}
}
vector<vector<int>> combine(int n, int k) {
backtracking(1, n, k);
return ans;
}
};