class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res = new ArrayList<>();
if(k==0||n<k){
return res;
}
Deque<Integer> path = new ArrayDeque<>();
dfs(n,k,1,path,res);
return res;
}
public void dfs(int n,int k,int begain,Deque<Integer> path,List<List<Integer>> res){
if(path.size()==k){
res.add(new ArrayList<>(path));
return;
}
for(int i=begain;i<=n;i++){
path.addLast(i);
dfs(n,k,i+1,path,res);
path.removeLast();
}
}
}
OJ---组合
最新推荐文章于 2025-05-24 10:42:46 发布