题解 | #简单错误记录#
简单错误记录
https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let errorDoumentURLArr = []; rl.on("line", function (line) { if (!errorDoumentURLArr.includes(line)) { errorDoumentURLArr.push(line); } }); rl.on("close", () => { const helperQueue = []; const helperMap = new Map(); const length = errorDoumentURLArr.length; for (let i = 0; i < length; i++) { const [errorDocumentURI, lineNum] = errorDoumentURLArr[i].split(" "); let errorDocument = errorDocumentURI.split("\\").pop(); if (errorDocument.length > 16) { const startIndex = errorDocument.length - 16; errorDocument = errorDocument.slice(startIndex); } const relErrorDocument = errorDocument + " " + lineNum; helperMap.set( relErrorDocument, (helperMap.get(relErrorDocument) || 0) + 1 ); // helperMap.get(relErrorDocument) < 2 表示如果队列里没有这个文件 但前面这个文件已经出现过了 就和前一个文件合并一起丢弃就不用计算在内了 if (!helperQueue.includes(relErrorDocument) && helperMap.get(relErrorDocument) < 2) { helperQueue.push(relErrorDocument); if (helperQueue.length > 8) { helperQueue.shift(); } } } for (const relErrorDocument of helperQueue) { console.log(relErrorDocument, helperMap.get(relErrorDocument)); } });