给定两个整数 n 和 k,返回 1 … n 中所有可能的 k 个数的组合。
示例:
输入: n = 4, k = 2
输出:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
class Solution {
public List<List<Integer>> combine(int n, int k) {
Deque<Integer> que = new LinkedList();
List<List<Integer>> res = new ArrayList();
back(que,1,n,k,res);
return res;
}
public void back(Deque<Integer> que,int start,int n,int k,List<List<Integer>> res){
if(que.size()==k){
res.add(new ArrayList(que));
}
for(int i=start;i<=n;i++){
que.offerLast(i);
back(que,i+1,n,k,res);
que.pollLast();
}
}
}