题解 | 简单密码

简单密码

https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

/**
 * 简单密码
 */

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        const strs = line.split("");
        const result = [];
        const keyboardMap = {
            abc: "2",
            def: "3",
            ghi: "4",
            jkl: "5",
            mno: "6",
            pqrs: "7",
            tuv: "8",
            wxyz: "9",
        };

        for (let i = 0; i < strs.length; i++) {
            const str = strs[i];
            const strCode = str.charCodeAt();

            if (!isNaN(Number(str))) {
                result.push(str);
            } else if (strCode >= 65 && strCode <= 90) {
                // 大写
                // 先转换为小写
                const strLower = str.toLowerCase();
                // 找出小写字母的ascii码
                if (strCode == 90) {
                    result.push("a");
                } else {
                    result.push(String.fromCharCode(strLower.charCodeAt() + 1));
                }
            } else if (strCode >= 97 && strCode <= 122) {
                // 小写
                Object.keys(keyboardMap).forEach((key) => {
                    if (key.includes(str)) {
                        result.push(keyboardMap[key]);
                    }
                });
            }
        }

        console.log(result.join(""));
    }
})();

#华为算法题##js解法##算法题##华为OD流程#
全部评论

相关推荐

2025-12-30 16:42
同济大学 C++
仁狂躁使者:哎呀,不用担心,我当时配环境配了两天,项目捋不清就问问导师能不能用ai,慢慢就清了,会好起来的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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