Skip to content

Instantly share code, notes, and snippets.

@varvir
Created March 6, 2020 04:40
Show Gist options
  • Save varvir/d561cfd5529c8717166fb017647aa5dc to your computer and use it in GitHub Desktop.
Save varvir/d561cfd5529c8717166fb017647aa5dc to your computer and use it in GitHub Desktop.
순열 기반 코드 쉬운 구현
List<List<Integer>> permute(List<Integer> input){
List<List<Integer>> result = new ArrayList<>();
permute(new ArrayList<>(), input, result);
return result;
}
void permute(List<Integer> remain, List<Integer> input, List<List<Integer>> result){
if(input.isEmpty()){
result.add(remain);
return;
}
else {
for(int i=0; i<input.size(); i++){
var temp = new ArrayList<Integer>(remain);
temp.add(input.get(i));
List<Integer> next = Stream.concat(input.subList(0, i).stream(), input.subList(i+1,input.size()).stream()).collect(Collectors.toList());
permute(temp, next, result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment