简单的dfs。
class Solution {
public:
vector<vector<int> > combine(int n, int k) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ans.clear();
dfs(n, k, 0);
return ans;
}
void dfs(int n, int k, int cnt){
if(cnt == k){
ans.push_back(v);
return ;
}
for(int i = 1; i <= n; i++){
if(v.empty() || i > v[v.size()-1]){
v.push_back(i);
dfs(n, k, cnt+1);
v.pop_back();
}
}
}
vector<vector<int> > ans;
vector<int> v;
};