题解 | #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
};
全部评论

相关推荐

点赞 评论 收藏
分享
昨天 17:14
中北大学 Java
点赞 评论 收藏
分享
05-13 02:01
已编辑
惠州学院 前端工程师
安静的少年在求佛:建议把公司名字写到标题。以后有人想搜就能直接搜到
点赞 评论 收藏
分享
野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
面试吐槽bot
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务