题解 | #全排列#
全排列
https://www.nowcoder.com/practice/b3ac35e1569e4601b6d3957dd337e70b
const _permute = (string) => { let flag = new Array(string.length).fill(0); const len = string.length; let result = []; let res = ""; function d() { if (res.length === len) { result.push(res); res = ""; // res.slice(-1); // flag = new Array(string.length).fill(0); return; } for (let i = 0; i < len; ++i) { if (!flag[i]) { let temp = res; flag[i] = 1; res = res + string[i]; d(); flag[i] = 0; res = temp; } } } d(); console.log(result); return result; };