题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
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 let input = []; while(line = await readline()){ input.push(line); } // 给的index值是索引,要求返回的值按照index升序,最好用数组去记录 let arr = []; input.shift(); // 输入的键值对集合 for (let item of input) { let key = item.split(' ')[0]; let value = item.split(' ')[1]; if (arr[key] === undefined) { arr[key] = parseInt(value); } else { arr[key] = arr[key] + parseInt(value); } } arr.forEach((item, index) => { if (item !== undefined) { console.log(`${index} ${item}`); } }) }()