Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
public class Solution {
public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> res=new LinkedList< List<Integer>>();
combine(res,n,1,k,new LinkedList<Integer>());
return res;
}
public void combine(List<List<Integer>> res,int n,int start,int k,LinkedList<Integer> a){
if(k==0){
res.add(a);
return;
}
for(int i=start;i<=n;i++){
LinkedList<Integer> list=(LinkedList<Integer>)a.clone();
list.add(i);
combine(res,n,i+1,k-1,list);
}
}
}
181

被折叠的 条评论
为什么被折叠?



