77. 组合
class Solution {
public:
vector<int> path;
vector<vector<int>> res;
void func(int n, int k, int st){
if(path.size() == k){
res.push_back(path);
return;
}
for(int i = st; i <= n-(k-path.size())+1; ++i){
path.push_back(i);
func(n,k,i+1);
path.pop_back();
}
}
vector<vector<int>> combine(int n, int k) {
func(n,k,1);
return res;
}
};
解题思路:
初见回溯,搞清楚套路就好些很多了。