If I develop a solution based on 16.5, wouldn’t it be easier to read than solution 16.6?
vector<vector<int>> Combinations(int n, int k) {
vector<vector<int>> result;
vector<int> ans;
CombinationsHelper(n, k, 1, &ans, &result);
return result;
}
void CombinationsHelper(int n, int k, int start, vector<int>* ans,
vector<vector<int>>* result) {
if (ans->size() == k) {
result->emplace_back(*ans);
return;
}
for (int i = start; i <= n; i++)
{
ans->emplace_back(i);
CombinationsHelper(n, k, i + 1, ans, result);
ans->pop_back();
}
}
The original solution produces subsets in descending order but the elements within the subsets are ascending, which makes the code a bit harder to follow.