题解 | #合并表记录#
合并表记录
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; //方法一:计数排序:您的程序使用了超过限制的内存,new Array(11111111+1)太大 // void async function(){ // let n = await readline(); // const cnt = new Array(11111111+1).fill(0); // while(n--){ // const [index,value] = (await readline()).split(" "); // cnt[index-0]+=value-0; // } // for(let i = 0; i <= 11111111; i++ ){ // if(cnt[i]) console.log(i,cnt[i]); // } // }() // 方法二:哈希表存储 void (async function () { let n = await readline(); const map = new Map(); while (n--) { const [index, value] = (await readline()).split(" ").map(Number); map.set(index,(map.get(index)||0)+value); } const res = [...map.entries()]; // console.log(res); res.sort((a,b)=>a[0]-b[0]); res.forEach((item) => console.log(item[0],item[1])); })();