持续分享牛客网华为解题大全

语言: java
牛客网华为机试地址

HJ1 字符串最后一个单词的长度
import java.util.*;

public class Main {
	// 解题思路: 求最后一个单词的长度, 那么只需要把最后一个单词提取出来即可. 
	// 怎么提取? 给两个标位, 首i, 尾j. j默认置为字符串下标0的位置, 此时反向遍历字符串, 当j==0的时候, 并且是字母, 就把单词末尾位置j, 置为此时下标.
	// 确认了单词末尾, 在确认单词首位, 继续遍历, 遇到空格, 就可以确定是首位, 如果没有遇到首位, 直接遍历到了字符串起始, 那么这整个字符串就是一个单词. 
	// 最后输出单词长度.
	// tips: 我之前写的是完整思路, 代码思路是可以简化的, 比如把字符串去空格后, j默认就可以置为字符串末尾
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine().trim();
        int j = 0; // 单词末尾
        char c;
        for (int i = str.length() - 1; i >= 0; i--) {
            c = str.charAt(i);
            if (j == 0) {
                j = i;//65~90
            } else if (c == ' ') {
                System.out.println(str.substring(i + 1, j + 1).length());
                break;
            } else if (i == 0) {
                System.out.println(str.substring(i, j + 1).length());
                break;
            }
        }
    }
}

在这里插入图片描述

HJ2 计算某字母出现次数
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] origin = sc.nextLine().toUpperCase().toCharArray();
        char target = sc.nextLine().toUpperCase().charAt(0);
        int result = 0;
        for (int i = 0; i < origin.length; i++) {
            if (target == origin[i]) {
                result++;
            }
        }
        System.out.println(result);
    }
}


在这里插入图片描述

HJ3 明明的随机数
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len, temp, i, j, k, j1, k1;
        int[] arr;
        while(sc.hasNext()){
            len = sc.nextInt();
            arr = new int[len];
            for ( i = 0; i < len; i++) {
                arr[i] = sc.nextInt();
                // 一边输入, 一边去重和排序 1 2 3 0 0 4
                for (j = i - 1, k = i; j >= 0; j--, k--) {
                    j1=arr[j];
                    k1=arr[k];
                    if (j1 > k1) {
                        temp = j1;
                        arr[j] = k1;
                        arr[k] = temp;
                     } else if (j1 == k1 && k1 > 0) {
                        arr[j] = 0;
                    }
                }
            }
            for (i = 0; i < len; i++) {
                if ((temp = arr[i]) > 0) {
                    System.out.println(temp);
                }
            }
        }
    }
}


在这里插入图片描述

HJ4 字符串分隔
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n, len;
        String temp;
        while (sc.hasNextLine()) {
            temp = sc.nextLine();
            if ((len = temp.length()) > 0) {
                if (len % 8 > 0) {
                    temp += "0000000";
                    n = len / 8 + 1;
                }else {
                    n = len / 8;
                }
                for (int i = 0; i < n; i++) {
                    System.out.println(temp.substring(i * 8, i * 8 + 8));
                }
            }
        }
    }
}

在这里插入图片描述

HJ5 进制转换
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String temp;
        char c;
        while (sc.hasNextLine()) {
            temp = sc.nextLine();
            int result = 0;
            for (int i = temp.length() - 1; i > 1; i--) {
                c = temp.charAt(i);
                if (c >= 65) {
                    result += (c - 55) * Math.pow(16, temp.length() - i - 1);
                } else {
                    result += (c - 48) * Math.pow(16, temp.length() - i - 1);
                }
            }
            System.out.println(result);
        }
    }
}


在这里插入图片描述

HJ6 质数因子
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        
       Scanner sc = new Scanner(System.in);
       int n = sc.nextInt();
       int temp = 0;

       while (n > 1) {
           if (n % 2 == 0) {
               n = n / 2;
               System.out.print(2 + " ");
               continue;
           }
           if (n == 3) {
               System.out.print(n + " ");
               return;
           } else {
               for (int i = 3; i < Math.sqrt(n) + 1; i += 2) {
                   temp = n;
                   if (n % i == 0) {
                       n = n / i;
                       System.out.print(i + " ");
                       break;
                   }
               }

               if (temp == n) {
                   System.out.print(n + " ");
                   return;
               }
           }
       }
       sc.close();
    }
}


在这里插入图片描述

HJ7 取近似值
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        Float n = sc.nextFloat();
        int temp;
        if (n - (temp = n.intValue()) >= 0.5) {
            System.out.println(temp + 1);
        } else {
            System.out.println(temp);
        }
    }
}

在这里插入图片描述
强转:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        System.out.println((int) (new Scanner(System.in).nextFloat() + .5));
    }
}

在这里插入图片描述

HJ8 合并表记录
import java.util.Scanner;
import java.util.TreeMap;

public class Main{
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        // 插入后, 变成有序的;
        TreeMap<Integer, Integer> treeMap = new TreeMap<>();
        for (int i = 0; i < n; i++) 
            treeMap.merge(sc.nextInt(), sc.nextInt(), Integer::sum);
        
        treeMap.forEach((k, v) -> System.out.println(k + " " + v));
    }
}

如何提升速度, 并且降低内存使用?求告知
在这里插入图片描述

HJ9 提取不重复的整数
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		LinkedHashSet<Character> set = new LinkedHashSet<>();
		for (int i = str.length() - 1; i >= 0; i--) {
			char c = str.charAt(i);
			set.add(str.charAt(i));
		}
		set.forEach(System.out::print);
    }
}

在这里插入图片描述
不用LinkedHashSet

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] chars = new char[str.length()];
        for (int i = str.length() - 1; i >= 0; i--) {

            chars[str.length() - i - 1] = str.charAt(i);
            for (int j = str.length() - i - 2; j >= 0; j--) {

                if (str.charAt(i) == chars[j] && chars[j] != ' ') {
                    chars[str.length() - i - 1] = ' ';
                    break;
                }
            }
        }
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != ' ') {
                System.out.print(chars[i]);
            }
        }
    }
}

在这里插入图片描述

HJ10 字符个数统计
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] chars = new char[str.length()];
        for (int i = str.length() - 1; i >= 0; i--) {

            chars[str.length() - i - 1] = str.charAt(i);
            for (int j = str.length() - i - 2; j >= 0; j--) {

                if (str.charAt(i) == chars[j] && chars[j] != ' ') {
                    chars[str.length() - i - 1] = ' ';
                    break;
                }
            }
        }
        int result = 0;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != ' ') {
                result++;
            }
        }
        System.out.println(result);
        
    }
}

在这里插入图片描述

import java.util.Scanner;
import java.util.HashSet;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        HashSet<Character> set = new HashSet<>();
        for (int i = 0; i < str.length(); i++) {
            set.add(str.charAt(i));
        }

        System.out.println(set.size());
        
    }
}

在这里插入图片描述

HJ11 数字颠倒
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
		
        for (int i = str.length()-1; i >= 0; i--){
            System.out.print(str.charAt(i));
        }
    }
}


在这里插入图片描述

HJ12 字符串反转
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
		
        for (int i = str.length()-1; i >= 0; i--){
            System.out.print(str.charAt(i));
        }
    }
}


在这里插入图片描述

HJ13 句子逆序
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine().trim();

        char c;
        for (int i = str.length() - 1, j = 0; i >= 0; i--) {
            c = str.charAt(i);
            if (c >= 65 && c <= 90 || (c >= 97 && c <= 122)) {
                if (j == 0) {
                    j = i + 1;
                }
                if (i == 0) {
                    System.out.print(str.substring(i, j));
                }
            } else if (c == ' ') {
                if (j == 0) {
                    System.out.print(" ");
                    continue;
                }
                System.out.print(str.substring(i + 1, j) + " ");
                j = 0;
            }
        }
    }
}


在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用,牛客网华为机试题解的JavaScript版本提供了第11到20题的解答。其中包括了数字颠倒、字符串反转、句子逆序、字符串排序、求int型数据在内存中存储时1的个数、购物单、坐标移动、识别有效的IP地址和掩码并进行分类统计、简单错误记录和密码验证合格程序。 根据引用,题目描述了如何将输入的整数以字符串的形式逆序输出。程序不考虑负数的情况,如果数字中包含0,逆序形式也会包含0。 根据引用,题目描述了如何计算输入的正整数在内存中存储时1的个数。题目要求输入一个整数(int类型),并将该数转换成二进制形式后输出其中1的个数。 需要注意的是,输入和输出的具体实现可能因题目而异,以上引用提供了一些示例代码,但并不代表所有题目的通用解法。正确的输入输出取决于具体题目的要求和所给代码的实现方式。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [JavsScript牛客网华为机试(11-20)题解](https://blog.csdn.net/weixin_43465339/article/details/110709521)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值