题解 | #字符串的排列#

字符串的排列

https://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param str string字符串 
 * @return string字符串一维数组
 */
function Permutation(str) {
    // write code here
    let res = []
    if (str.length === 0) return []
    if (str.length === 1) return [str]
    //发现case:"aa"不通过,加去重
    if (str.length === 2) return [...new Set([str[0] + str[1], str[1] + str[0]])]
    for (let i = 0; i < str.length; i++) {
        let curStr = str[i]
        let rest = str.slice(0, i) + str.slice(i + 1, str.length)
        let restRes = Permutation(rest)
        for (let j = 0; j < restRes.length; j++) {
            res.push(curStr + restRes[j])
        }
    }
    //同理
    return [...new Set(res)]
}
module.exports = {
    Permutation : Permutation
};

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 14:23
steelhead:你回的有问题,让人感觉你就是来学习的
点赞 评论 收藏
分享
点赞 评论 收藏
分享
小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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