謎題
窮舉一個陣列中各個元素的排列
策略
減而治之、遞歸
JavaScript解
function getPermutation(arr) {
if (arr.length == 1) {
return [arr];
}
var permutation = [];
for (var i=0; i
var arrClone = arr.slice(0);
arrClone.splice(i, 1);
var childPermutation = getPermutation(arrClone);
for (var j=0; j
}
permutation = permutation.concat(childPermutation);
}
return permutation;
}
var permutation = getPermutation(['a','b','c']);
console.dir(permutation);
結果