牛客网:华为机试 第二话——HJ8~HJ5

77 阅读2分钟

第二话:HJ8~HJ15

HJ8 合并表记录

合并表记录

解题思路 & 编码实现

将输入的n个键值对对应进行分割,下标index已存在的,对应值为arrRes[index] = arrRes[index] + values; 下标index不存在的,对应值为arrRes[index] = 0 + values。最后按照下标(对应index值)遍历输出。

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
    while ((line = await readline())) {
        let n = parseInt(line);
        // 保存对象数组
        let arrRes = [];
        for (let i = 0; i < n; i++) {
            line = await readline();
            line = line.split(" ");
            // 分割 键-值
            let index = parseInt(line[0]);
            let values = parseInt(line[1]);
            arrRes[index] = (arrRes[index] || 0) + values;
        }
        // 数组保存,下标已经有序
        for (let i = 0; i < arrRes.length; i++) {
            if (arrRes[i] !== undefined) {
                console.log(i + " " + arrRes[i]);
            }
        }
    }
})();

HJ9 提取不重复的整数

image.png

解题思路 & 编码实现

将输入的字符串转化为数组,进行反转,再将生成的数组存入集合中(去重),对去重后的数组使用join拼接

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
    while(line = await readline()){
        // 将输入的字符串转化为数组,进行反转,再将生成的数组存入集合中(去重),对去重后的数组使用join拼接
        line = [...new Set([...line].reverse())].join("")
        console.log(line);
    }
}()

HJ10 字符个数统计

YN`(F17TB}~S7_@OJ)66U.png

解题思路 & 编码实现

  1. 生成的数组存入集合中(去重),输出集合长度
  2. 采用数组的 indexOf() 函数,已存在该元素即不放入,达到去重效果
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
    while ((line = await readline())) {
        let res = [];
        for (let i = 0; i < line.length; i++) {
            if (res.indexOf(line[i]) === -1) {
                res.push(line[i]);
            }
        }
        console.log(res.length);
    }
})();

HJ11 数字颠倒

image.png

解题思路 & 编码实现

去掉字符串最后面空格,分割成数组,反转字符串数组,拼接返回

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
    while ((line = await readline())) {
        let arr = line.trim().split("").reverse();
        let res = arr.join("");
        console.log(res);
    }
})();

HJ12 字符串反转

image.png

解题思路 & 编码实现

倒序遍历数组,或者用数组的reverse方法实现反转。

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
    while(line = await readline()){
        let res = "";
        for(let i = line.length - 1; i >= 0; i--) {
            res += line[i];
        }
        console.log(res);
    }
}()

HJ13 句子逆序

RLRP4(W_2R.png

解题思路 & 编码实现

跟HJ12类似,将字符串分割成单词数组,倒序遍历数组,拼接成逆序的句子。或者用数组的reverse方法实现反转。

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
    while(line = await readline()){
        let tokens = line.split(' ');
        let res = "";
        for(let i = tokens.length - 1; i >= 0; i --) {
            res += tokens[i] + " ";
        }
        console.log(res);
    }
}()

HJ14 字符串排序

[U46~GXX3WO89E$0C5]CUJO.png

解题思路 & 编码实现

字典序排序,使用数组的sort函数。

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // n的范围在1,1000
    let n = 0;
    let data = []; 
    // Write your code here
    while(line = await readline()){
        if(n === 0){
            // 字符串转整数
            n = parseInt(line);
        }else {
            data.push(line);
        }
    }
    data.sort();
    // foreach遍历
    data.forEach(function(index){
        console.log(index)
    })
}()

HJ15 求int型正整数在内存中存储时1的个数

image.png

解题思路 & 编码实现

Number(line).toString(2):这个数转换成2进制。将二进制的字符串分割成数组,foreach遍历统计二进制中1的个数。

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    let n = [];
    let num = 0;
    // Write your code here
    while(line = await readline()){
        n = Number(line).toString(2).split("");
    }
    n.forEach(function(index){
        if(index === "1") {
            num ++;
        }
    })
    console.log(num)
}()