题解 | #字符串加解密#

字符串加解密

https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a

占用内存拉满,运行速度还行
function fn(str, isAdd = true) {
    const arr = str.split('')
    for(let i in arr) {
        const ascii = arr[i].charCodeAt()
        const val = isAdd ? 1 : -1
        const index = isAdd ? 0 : 2
        const a = [
            ['z','A','a','Z'],['Z','a','A','z'],['9','0','0','9']
        ]
        if(/[a-z]/.test(arr[i])) {
            arr[i] = arr[i] !== a[0][index] ?  String.fromCharCode(ascii - 32 + val) : a[0][index+1]
        } else if(/[A-Z]/.test(arr[i])) {
            arr[i] = arr[i] !== a[1][index] ?  String.fromCharCode(ascii + 32 + val) : a[1][index+1]
        } else if(/[0-9]/.test(arr[i])) {
            arr[i] = arr[i] !== a[2][index] ?  String.fromCharCode(ascii + val) : a[2][index+1]
        }
    }
    return arr.join('')
}
console.log(fn(readline()))
console.log(fn(readline(), false))


全部评论

相关推荐

只因飞飞:今日首绷
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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