您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

华为机试刷题日记

   

1、字符串最后一个单词的长度

因为题目要求是获取最后一个单词,所以顺着字符串从后往前查找第一个空格,查找到了第一个空格就知道了当前的长度了

const lineString = readline();

function getLastWordLenghth(str){

    let i = str.length-1;

    for(;i>=0;i--){

        if(str[i] === ' ') break;

    }

    return str.length - 1 -i;

}

console.log(getLastWordLenghth(lineString));

2、计算某字符出现次数

使用哈希表,需要注意的是需要考虑不区分大小写以及

let line1 = readline();

let line2 = readline();

function getHashMap(str){

    let map = {};

    for(let i = 0;i<str.length;i++){

        let code = str[i].toLocaleUpperCase()

        if(code in map){

            map[code]++;

        }else{

            map[code] = 1;

        }

    }

    return map;

}

let map = getHashMap(line1);

if(map[line2.toLocaleUpperCase()]){

console.log(map[line2.toLocaleUpperCase()]);    

}else{

console.log(0);    

}

3、HJ4 字符串分隔

递归实现

let line = readline();

function getEightStr(str){

    if(str.length>8){

        console.log(str.substring(0,8));

        getEightStr(str.substring(8))

    }else{

        console.log(str.padEnd(8,'0'));

    }

}

getEightStr(line);

4、HJ5 进制转换

let line = readline();

console.log(parseInt(line,16))

5、四舍五入

注意florr ceil round的使用

let line = readline();

console.log(Math.round(line));

6、HJ9 提取不重复的整数

注意map的使用has,set, get,delete

set集合,不保存值,has, add,delete

let line = readline();

let string = line+'';

let result = '';

let map = new Map();

for(let i = string.length-1;i>=0;i--){

    if(!map.has(string[i])){

        result+=string[i];

        map.set(string[i]);

    }

}

console.log(parseInt(result));

let line = readline();

let string = line+'';

let result = '';

let set = new Set();

for(let i = string.length-1;i>=0;i--){

    if(!set.has(string[i])){

        result+=string[i];

        set.add(string[i]);

    }

}

console.log(parseInt(result));

7、HJ46 截取字符串

let string = readline();

let num = readline();

console.log(string.substring(0,num));

8、HJ58 输入n个整数,输出其中最小的k个

let line1 = readline();

line1 = line1.split(' ');

let n = parseInt(line1[0]),k = parseInt(line1[1]);

let array = readline().split(' ');

array.sort((a,b) => a-b);

let result = array.splice(0,k).join(' ');

console.log(result);

9、HJ101 输入整型数组和排序标识,对其元素按照升序或降序进行排序

let n = readline();

let array = readline().split(' ');

let flag = readline();

function sortByFlag(array,flag){

    return array.sort((a,b)=>{

        if(flag == 1){

            return b-a;

        }else{

            return a-b;

        }

    }).join(' ');

}

console.log(sortByFlag(array,flag));

10、HJ10 字符个数统计

let string = readline();

let set = new Set();

let number = 0;

for(let i = 0;i<string.length;i++){

    if(!set.has(string[i])){

        set.add(string[i]);

        number++;

    }

}

console.log(number);

11、HJ3 明明的随机数

let n = readline();

let set = new Set();

for(let i = 0;i<=n;i++){

    set.add(readline());

}

let array = Array.from(set);

array = array.sort((a,b)=>a-b).join('\n');

console.log(array);

12、质数因子

需要注意的,质因子就是这个数从2开始不断的被整除,并且质因子的平方要小于这个数

let n = parseInt(readline());

let result = [],i = 2;

for(let i = 2;i <= n && i * i <= n;i++){

    while(n != i){

        if(n % i == 0){

              result.push(i);

                n = n/i;

        }else{

            break;

        }

    }

}

if(n!=1){

    result.push(n);

}

console.log(result.join(' '));

12、HJ8 合并表记录

let n = readline();

let map = {};

for(let i = 0;i<n;i++){

    let line = readline().split(' ');

    if(map[line[0]]){

        let old = map[line[0]]

        map[line[0]] = (old + +line[1]);

    }else{

        map[line[0]] = (+line[1]);

    }

}

let result = '';

for(let item in map){

    result = result + item +' '+map[item] +'\n';

}

console.log(result);

13、HJ11 数字颠倒

let number = readline()+'';

number = number.split('');

console.log(number.reverse().join(''))

14、HJ12 字符串反转

console.log(readline().split('').reverse().join(''))

15\HJ13 句子逆序

let string = readline();

string = string.split(' ');

string = string.reverse();

console.log(string.join(' '));

16\HJ14 字符串排序

let n = readline();

let array = [];

for(let i = 0;i<n;i++){

    array.push(readline());

}

array = array.sort();

console.log(array.join('\n'))

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

let number = parseInt(readline());

let string = number.toString(2).split('');

let n = 0;

for(let item in string){

    if(string[item] == 1){

        n++;

    }

}

console.log(n);

18、HJ21 简单密码

let line = readline();

let result = ''

for(let i = 0;i<line.length;i++){

    let str = line[i]

    if(str >= 'A' && str < 'a'){

        //大写字母 转小写然后+1

        str = str.toLocaleLowerCase();

        let code = str.charCodeAt();

        if(code < 'z'.charCodeAt()){

            result += String.fromCharCode(code+1) ;

        }else{

            result += 'a'

        }

        

        

    }else if(str >= 'a' && str <= 'z' ){

        //小写字母

        if(str == 'a' || str == 'b' || str == 'c'){

            result += '2';

        }else if(str == 'd' || str == 'e' || str == 'f'){

            result += '3';

        }else if(str == 'g' || str == 'h' || str == 'i'){

            result += '4';

        }else if(str == 'j' || str == 'k' || str == 'l'){

            result += '5';

        }else if(str == 'm' || str == 'n' || str == 'o'){

            result += '6';

        }else if(str == 'p' || str == 'q' || str == 'r' || str == 's'){

            result += '7';

        }else if(str == 't' || str == 'u' || str == 'v'){

            result += '8';

        }else{

            result += '9';

        }

        

    }else{

        //数字 原样输出

        result += str

    }

}

console.log(result);

19、HJ17 坐标移动

let string = readline();

string = string.split(';');

let x = 0,y = 0;

for(let i in string){

    let s = string[i];//获取当前坐标

    let q = s[0];

    let p = +(s.substring(1));

    if(!isNaN(p) && s.length>=2){

        if(q == 'A'){

            x -= p;

        }else if(q == 'D'){

            x += p;

        }else if(q == 'W'){

            y += p;

        }else if(q == 'S'){

            y -= p;

        }

    }

}

console.log(x+','+y)

20、HJ20 密码验证合格程序

while(line = readline()){

    //循环读入

   console.log(validate(line))

    

    

}

function validate(str){

    let result = 'NG';

     if(str.length > 8 && typeTest(str) && !isRepeat(str)){

       result = 'OK'

    }

   

    return result;

}

function isRepeat(str){

   

    for(let i = 0;i<str.length - 3;i++){

        let sub = str.substring(i,i+3);

        if(str.lastIndexOf(sub) > i){

            return true;

        }

    }

}

function typeTest(str){

    let num = 0;

    if(/[a-z]/.test(str)){

        num++;

    }

    if(/[A-Z]/.test(str)){

        num++;

    }

    if(/[0-9]/.test(str)){

        num++;

    }

    if(/[^0-9a-zA-z]/.test(str)){

        num++;

    }

    if(num>=3){

        return true

    }else{

        return false;

    }

}

21HJ23 删除字符串中出现次数最少的字符

let map = [];

let string  = readline();

for(let i = 0;i<string.length;i++){

    if(map[string[i]]){

        map[string[i]] = map[string[i]] + 1;

    }else{

        map[string[i]] = 1;

    }

}

let min = 20

for(let item in map){

    let v = map[item]

    if(v < min){

        min = v

    }

}

for(let item in map){

    if(map[item] == min){

        let reg = new RegExp(item,'g');

        string = string.replace(reg,'')

    }

}

console.log(string)

HJ26 字符串排序

while(line = readline()){

   let arr = line.split('');

    let result = [];

    for(let i = 0;i<26;i++){

        //根据字母表进行排序,不区分大小写

        for(let j = 0;j<arr.length;j++){

            if(arr[j].charCodeAt(0) == 65 +i || arr[j].charCodeAt(0) == 97+i){

                result.push(arr[j])

            }

        }

    }

    for(let i =0;i<arr.length;i++){

        if(/[^a-zA-Z]/g.test(arr[i])){

            result.splice(i,0,arr[i])

        }

    }

    console.log(result.join(''))

}

HJ35 蛇形矩阵

let n = readline()  

let result = []

for(let i = 0;i<n;i++){

   let array = new Array(n-i)

    for(let j = 0;j<n-i;j++){

       if(i == 0){

          if(j == 0){

               array[0] = 1

          }else{

               array[j] = array[j-1]+j+1

          }

                

        }else{

           array[j] = result[i-1][j+1] -1;

        }

           

     }

        result.push(array);

        console.log(array.join(' '));

  }

HJ58 输入n个整数,输出其中最小的k个

let line1 = readline().split(' ');

let array = readline().split(' ');

let n = line1[0],k = line1[1];

console.log(array.sort((a,b)=>a-b).slice(0,k).join(' '))


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进