分析:
- 循环的同时,利用递归,排除当前元素,获取其他元素的组合,然后再与当前元素进行组装
function fullpermutate(str) {
let result = [];
if (str.length == 1) {
result.push(str);
} else if (str.length > 1) {
for (let index = 0, len = str.length; index < len; index ) {
let current = str[index];
let rest = str.slice(0, index) str.slice(index 1, str.length);
// 上一次递归返回的全排列
let preResult = fullpermutate(rest);
// 组合
for (let index = 0, len = preResult.length; index < len; index ) {
let tmp = current preResult[index];
result.push(tmp);
}
}
}
return result;
}
console.log('abc:', fullpermutate("abc")); // [ 'abc', 'acb', 'bac', 'bca', 'cab', 'cba' ]