class Solution {
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> ans;
vector<int> res;
dfs(n,k,res,ans,1);
return ans;
}
void dfs(int n,int k,vector<int>& res,vector<vector<int>>& ans,int idx){
if(res.size()==k){
ans.push_back(res);
return;
}
for(int i=idx;i<=n;++i){
res.push_back(i);
dfs(n,k,res,ans,i+1);//idx+1则包含自身,如(1,1),(2,2)重复
res.pop_back();
}
}
};
leetcode 77 (1-n)k个数字组合
最新推荐文章于 2024-07-05 16:42:37 发布