JS 全排列是一道常见的前端算法题, backtrack 是解法之一 类似的题还有八皇后等 此题默认无重复字符
function permution (str) {
let results = [];
let strArr = str.split('');
let cur = [];
search(strArr, cur, results);
return results;
}
function search (strArr, cur, results) {
if (cur.length === strArr.length) {
results.push(cur.slice(0));
return;
}
for (let i = 0; i < strArr.length; i++) {
if (cur.indexOf(strArr[i]) === -1) {
cur.push(strArr[i]);
search(strArr, cur, results);
cur.pop();
}
}
}
复制代码