77.组合
class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> ans = new ArrayList<>();
if( k <= 0 || n < k){
return ans;
}
Deque<Integer> list = new ArrayDeque<>();
dfs(ans,list,n,k,1);
return ans;
}
private void dfs(List<List<Integer>> ans,Deque<Integer> list,int n,int k,int begin){
if(list.size() == k){
ans.add(new ArrayList<>(list));
return;
}
for(int i = begin; i<= n - (k - list.size()) + 1 ;i++){
if(list.size() != k){
list.addLast(i);
}
dfs(ans,list,n,k,i+1);
list.removeLast();
}
}
}