此题利用递归回溯思想
代码
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int> > result;
if(n<k||k==0)
return result;
vector<int> temp;
combineHelper(result, temp, n, k, 1);
return result;
}
void combineHelper(vector<vector<int> > &result, vector<int> temp, int n, int k, int start)
{
if(temp.size()==k)
{
result.push_back(temp);
return ;
}
for(int i = start; i <= n; ++i)
{
temp.push_back(i);
combineHelper(result, temp, n, k, i+1);
temp.pop_back();
}
}
};