Skip to content

Instantly share code, notes, and snippets.

@varvir
Last active March 21, 2020 03:24
Show Gist options
  • Save varvir/8aa16d033ac6b7caaf8d0bdb6ee01ad3 to your computer and use it in GitHub Desktop.
Save varvir/8aa16d033ac6b7caaf8d0bdb6ee01ad3 to your computer and use it in GitHub Desktop.
Latest version
List<List<Integer>> combination(int[] n, int r){
List<List<Integer>> ret = new ArrayList<>();
combination(new ArrayList<>(), n, r, ret);
System.out.println(ret);
return ret;
}
void combination(List<Integer> prefix, int[] n, int r, List<List<Integer>> result){
if(r==0) result.add(prefix);
else {
for(int k=0; k<=n.length-r; k++){
ArrayList<Integer> prefixcopy = new ArrayList<>(prefix);
prefixcopy.add(n[k]);
combination(prefixcopy, Arrays.copyOfRange(n, k+1, n.length), r-1, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment