题解 | #字符串加解密#
字符串加解密
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))