题解 | 简单密码

简单密码

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流程#
全部评论

相关推荐

11-27 14:21
同济大学 Java
卢来猴祖:给了这薪资关键拿不了几个月就给你踹了呀
点赞 评论 收藏
分享
11-28 16:13
门头沟学院 Java
程序员小白条:年底了,都差不多了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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