Skip to content

Instantly share code, notes, and snippets.

@morishjs
Created August 30, 2016 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morishjs/b5a830b2d43a83b67db4954274eb4aae to your computer and use it in GitHub Desktop.
Save morishjs/b5a830b2d43a83b67db4954274eb4aae to your computer and use it in GitHub Desktop.
//기본 조건 : 현재 위치가 마지막 문자이면 출력
//재귀 조건 : 현재 위치에서 선택하지 않은 문자를 선택하고 다음 위치에서 새로운 문자를 선택하도록 함.
var stringArr = ['A', 'B', 'C', 'D'];
var exist = [];
for(var i = 0; i < stringArr.length;i++){
exist.push(false);
}
//n : 현재위치
//arr : 현재 만들어진 문자열
function permutation(arr, n){
//기본 조건
if(n == stringArr.length) {
console.log(arr)
return;
}
//재귀조건
for(var i=0;i<exist.length;i++) {
//이미 문자열에 포함되어있다.
if(exist[i] == true) {
continue;
}
else{
arr.push(stringArr[i]);
exist[i] = true;
permutation(arr,n+1);
exist[i] = false;
arr.pop();
}
}
}
permutation([], 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment