题解 | #合并表记录#
合并表记录
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 lines = [];
while(line = await readline()){
lines.push(line);
}
let obj = {};
// map比array方便,合并重复key不需要遍历,输出也方便
for (let i = 1; i < lines.length; i++) {
let vals = lines[i].split(' ');
let key = vals[0] + '';
let val = vals[1];
obj[key] = !obj[key] ? 0 : obj[key];
obj[key] = obj[key] + val * 1;
}
for (key in obj) {
console.log(key+ ' ' + obj[key])
}
}()
查看1道真题和解析
美团公司福利 3020人发布