Skip to content

Instantly share code, notes, and snippets.

@varvir
Created March 21, 2020 04:13
Show Gist options
  • Save varvir/808739dd49f59fb0bcb29ea2fcbb6441 to your computer and use it in GitHub Desktop.
Save varvir/808739dd49f59fb0bcb29ea2fcbb6441 to your computer and use it in GitHub Desktop.
int[]로 주어질 때
List<List<Integer>> permutation(int[] n){
List<List<Integer>> ret = new ArrayList<>();
permutation(new ArrayList<>(), n, ret);
System.out.println(ret.size());
return ret;
}
void permutation(List<Integer> prefix, int[] n, List<List<Integer>> result){
if(n.length==0){
result.add(prefix);
} else {
for(int k=0; k<n.length; k++){
var tmp = new ArrayList<Integer>(prefix);
tmp.add(n[k]);
int[] next = Stream.concat(Arrays.stream(Arrays.copyOfRange(n, 0, k)).boxed(),
Arrays.stream(Arrays.copyOfRange(n, k+1, n.length)).boxed())
.mapToInt(i->i).toArray();
permutation(tmp, next, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment