From d8abbca4a877c4acd34744add2c69f940a4c5e38 Mon Sep 17 00:00:00 2001 From: qjd1774 Date: Thu, 18 Jan 2024 11:32:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0kama54.=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=95=B0=E5=AD=97.md=20Javascript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kama54.替换数字.md | 53 +++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/problems/kama54.替换数字.md b/problems/kama54.替换数字.md index 2b3d53de..130c42f7 100644 --- a/problems/kama54.替换数字.md +++ b/problems/kama54.替换数字.md @@ -245,7 +245,60 @@ class Solution: return ''.join(lst) ``` ### JavaScript: +```js +const readline = require("readline"); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}) + +function main() { + const num0 = "0".charCodeAt(); + const num9 = "9".charCodeAt(); + const a = "a".charCodeAt(); + const z = "z".charCodeAt(); + function isAZ(str) { + return str >= a && str <= z; + } + function isNumber(str) { + return str >= num0 && str <= num9; + } + rl.on("line", (input) => { + let n = 0; + for (let i = 0; i < input.length; i++) { + const val = input[i].charCodeAt(); + if (isNumber(val)) { + n+= 6; + } + if (isAZ(val)) { + n++; + } + } + const ans = new Array(n).fill(0); + let index = input.length - 1; + for (let i = n - 1; i >= 0; i--) { + const val = input[index].charCodeAt(); + if (isAZ(val)) { + ans[i] = input[index]; + } + if (isNumber(val)) { + ans[i] = "r"; + ans[i - 1] = "e"; + ans[i - 2] = "b"; + ans[i - 3] = "m"; + ans[i - 4] = "u"; + ans[i - 5] = "n"; + i -= 5; + } + index--; + } + console.log(ans.join("")); + }) +} + +main(); +``` ### TypeScript: