https://leetcode.com/problems/combinations/
class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> rst;
vector<int> sol;
combine(n, k, 1, sol, rst);
return rst;
}
void combine(int n, int k, int start, vector<int> &sol, vector<vector<int>> &rst) {
if (sol.size() == k) {
rst.push_back(sol);
return;
}
for (int i = start; i <= n; i++) {
sol.push_back(i);
combine(n, k, i+1, sol, rst);
sol.pop_back();
}
}
};
网友评论