05_全排列

本题考点:回溯、递归

根据题目要求,返回字符串参数的所有可能的排列组合,核心步骤有:

  1. 创建返回的结果数组
  2. 通过字符串参数创建等长的"被使用"数组用于递归过程中记录字符顺序
  3. 创建回溯函数,通过该函数内部递归调用
  4. 在回溯函数中,当临时数组的长度等于字符串参数长度时可以返回本次循环结果
  5. 进入字符串参数长度的循环体中,将该次字符保存在临时数组中
  6. 将该次字符的"被使用"数组位修改为true,表示该字符在本次之前的递归过程中已被记录使用,跳过即可
  7. 递归调用回溯函数
  8. 退栈时设置该字符"被使用"数组为false,删除临时数组最后一位
  9. 返回结果

参考答案

const _permute = string => {
    const result = []
    const used = Array.from({length: string.length}, () => false)
    const _backTrack = (candidate, memo = []) => {
        if(memo.length === string.length) {
            result.push(memo.slice().join(''))
            return
        }
        for(let i=0 ; i<candidate.length ; i++) {
            if(used[i]) continue
            memo.push(candidate[i])
            used[i] = true
            _backTrack(candidate, memo)
            used[i] = false
            memo.pop()
        }
    }
    _backTrack(string)
    return result
}