题解 | #合并表记录#

合并表记录

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])
    }
}()

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务