解答这题巧妙运用了递归、回溯,可以对回溯有个更好的理解
https://blog.csdn.net/lindaxym/article/details/80951872
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
vector<vector<int>>vv;
if(n<k||n<1||k<1){
return vv;
}
vector<int>v;
findCombine(1,n,k,v,vv);
return vv;
}
void findCombine(int x,int &n,int &k,vector<int> &v,vector<vector<int>> &vv){
if(v.size()==k){
vv.push_back(v);
return ;
}
for(int i=x;i<=n;i++){
v.push_back(i);
findCombine(i+1,n,k,v,vv);
v.pop_back();
}
return ;
}
};