public List<List<Integer>> Combin(int n, int k){
List<List<Integer>> res = new ArrayList<List<Integer>>();
backtrack(res, new ArrayList<Integer>(), n, k, 1);
return res;
}

public void backtrack(List<List<Integer>> res, List<Integer> tempList, int n, int k, int start){
if(tempList.size()==k){
 res.add(new ArrayList<>(tempList));
 }
else if(tempList.size()>k) return;
else{
 for(int i = start;i<=n;i++){
 tempList.add(i);
 backtrack(res,tempList,n,k,i+1);
 tempList.remove(tempList.size()-1); 
  }
 }
}

results matching ""

    No results matching ""