如果您注意到,代码实际上会在进行任何置换之前将字符拆分成数组,因此您只需删除联接和拆分操作
var permArr = [], usedChars = [];function permute(input) { var i, ch; for (i = 0; i < input.length; i++) { ch = input.splice(i, 1)[0]; usedChars.push(ch); if (input.length == 0) { permArr.push(usedChars.slice()); } permute(input); input.splice(i, 0, ch); usedChars.pop(); } return permArr};document.write(JSON.stringify(permute([5, 3, 7, 1])));


