Q. N개의 숫자로 이루어진 배열를 받았을 때
조합할 수 있는 순열 경우의 수를 모두 구하자.
function doTree(input) {
let doTree_result = [];
let result = [];
if (input.length > 3) {
for (let i = 0; i < input.length; i++) {
let prev = 1;
let copyArr = [...input];
prev = copyArr.splice(i, 1);
doTree_result = doTree(copyArr);
for (let j = 0; j < doTree_result.length; j++) {
doTree_result[j].splice(0, 0, prev);
console.log(`doTree_result[${j}] : ${doTree_result[j]}`);
result.push(doTree_result[j]);
}
}
}
if (input.length === 3) {
return swap(input);
}
return result;
}
function swap(arr) {
let result = [];
let start;
for (let i = 0; i < arr.length; i++) {
let copyArr = [...arr];
start = copyArr.splice(i, 1);
result.push([start, copyArr[0], copyArr[1]]);
result.push([start, copyArr[1], copyArr[0]]);
}
return result;
}
let input = [1, 2, 3, 4, 5];
let qResult = doTree(input);
console.log(`최종값은? ${qResult} / 갯수는? ${qResult.length}`);