题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
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()){ let arr = line.split(';') let directionMap = { 'A': { idx: 0, multiple: -1 }, 'D': { idx: 0, multiple: 1 }, 'W': { idx: 1, multiple: 1 }, 'S': { idx: 1, multiple: -1 } } let res = [0, 0] for (let val of arr) { if (/^([A|S|W|D])(\d+)$/.test(val)) { let [_, direction, num] = val.match(/([A|S|W|D])(\d+)/) num = Number(num) let {idx, multiple} = directionMap[direction] res[idx] += (multiple * num) } } console.log(res.join(',')) } }()
主要点: 1正确匹配字符串
2.字符串分割
#算法#