题解 | #JS_字符串的排列#
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
回溯+剑指
function Permutation(str) { // write code here let used = new Array(str.length).fill(false); let arr = str.split('').sort(); let tmp = ''; let res = []; const dfs = () => { if(tmp.length === arr.length){ res.push(tmp); return } for(let i = 0;i < str.length;i++){ if(used[i]) continue; if(i > 0 && arr[i] === arr[i-1] && used[i-1]) continue tmp += arr[i]; used[i] = true; dfs(); tmp = tmp.slice(0,tmp.length-1); used[i] = false; } } dfs(); return res; } module.exports = { Permutation : Permutation };