题解 | #合并表记录#注意Map和Array之间的转换
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let total = 0; let map = new Map(); let k = -1; rl.on('line', function(line){ if(k === -1){ total = Number(line); k = 0 }else if(k < total) { let [key, value] = line.split(' '); key = Number(key); value = Number(value); if(map.has(key)){ map.set(key, map.get(key) + value); }else{ map.set(key, value); } k++; } if(k === total){ let arr = Array.from(map); arr.sort((a,b)=>a[0]-b[0]); let map2 = new Map(arr); map2.forEach((value, key)=>{ console.log(key + ' ' + value); }); } });
#华为笔试#