题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

const readline = require('readline');

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const lines = [];

rl.on('line', function (line) {
    const newLine = line.split(' ');
    if (newLine.length === 2) {
        const index = lines.findIndex((item) => item.index === newLine[0]);
        if (index > -1) {
            lines[index].value += parseInt(newLine[1]);
        } else {
            lines.push({
                index: newLine[0],
                value: parseInt(newLine[1]),
            })
        }
    }
});

rl.on('close', () => {
    lines.sort((a, b) => parseInt(a.index) - parseInt(b.index));
    lines.forEach((item) => console.log(`${item.index} ${item.value}`));
})

全部评论

相关推荐

04-30 21:35
已编辑
长安大学 C++
晓沐咕咕咕:评论区没被女朋友好好对待过的计小将可真多。觉得可惜可以理解,毕竟一线大厂sp。但是骂楼主糊涂的大可不必,说什么会被社会毒打更是丢人。女朋友体制内生活有保障,读研女朋友还供着,都准备订婚了人家两情相悦,二线本地以后两口子日子美滋滋,哪轮到你一个一线城市房子都买不起的996清高计小将在这说人家傻😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务