题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
rl.on("line", function (line) {
    const arr = line.split("");
    const len = arr.length;
    let sorted = new Array(26).fill("");

    arr.forEach(item=>{
        let x = item.charCodeAt()
        if(x>=65 && x<=90){
            // 代表是 A-Z
            sorted[x-65] += item
        }
        // a-z
        if(x>=97 && x <= 122){
            sorted[x-97] += item
        }
    })
    // console.log(sorted,"---")
     sorted = sorted.join('').split('')
    for (let i = 0; i < len; i++) {
        if (/[^a-zA-Z]/.test(arr[i])) {
            // 数组的插入方法
            sorted.splice(i, 0, arr[i]);
        }
    }
    console.log(sorted.join(""))
});

全部评论

相关推荐

头像
05-16 11:16
已编辑
东华理工大学 Java
牛客737698141号:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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