题解 | #字符串排序#

字符串排序

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(""))
});

全部评论

相关推荐

07-19 13:28
长沙学院 Java
程序员小白条:你有面试就有希望,没面试自然就没希望,到时候就知道了,你问别人也没啥用处的
点赞 评论 收藏
分享
07-24 19:01
门头沟学院 Java
后天笔试,又要开始做题了
Sairus:明天10:00笔试
投递京东等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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