题解 | #数据分类处理#

数据分类处理

https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd

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 index = 0;
    let arrI = [],
        arrR = [];
    while ((line = await readline())) {
        if (index === 0) {
            arrI = line.split(" ");
            arrI.shift();
            index++;
        } else {
            arrR = line.split(" ");
            arrR.shift();
        }
    }

    arrR = Array.from(new Set(arrR));
    arrR.sort((a, b) => a - b);

    let hashArray = new Map();

    for (let r = 0; r < arrR.length; r++) {
        let indexArr = [];
        let itemArr = [];
        let obj;
        for (let i = 0; i < arrI.length; i++) {
            if (arrI[i].includes(arrR[r])) {
                indexArr.push(i);
                itemArr.push(arrI[i]);
                if (hashArray.has(arrR[r])) {
                    obj.num += 1;
                    obj.index = indexArr;
                    obj.item = itemArr;
                } else {
                    obj = {
                        num: 1,
                        index: indexArr,
                        item: itemArr,
                    };
                }
                hashArray.set(arrR[r], obj);
            }
        }
    }

    let key = hashArray.keys();
    let finalArray = [];
    while ((k = key.next().value)) {
        let value = hashArray.get(k);
        finalArray.push(k);
        finalArray.push(value.num);
        for (let i = 0; i < value.index.length; i++) {
            finalArray.push(value.index[i]);
            finalArray.push(value.item[i]);
        }
    }
    let len = finalArray.length.toString();
    console.log(len.concat(" ", finalArray.join(" ")));
})();

全部评论

相关推荐

10-07 20:48
门头沟学院 Java
听说改名就会有offer:可能是实习上着班想到后面还要回学校给导师做牛马,看着身边都是21-25的年纪,突然emo了了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务