【Java】牛客网华为机试高频算法题精解

张开发
2026/4/12 13:33:25 15 分钟阅读

分享文章

【Java】牛客网华为机试高频算法题精解
1. 华为机试高频算法题解析指南作为Java开发者想要进入华为这样的科技巨头机试是必须跨过的一道门槛。我在准备华为机试时发现牛客网上有几类算法题出现的频率特别高比如字符串处理、数学运算、数据结构应用等。这些题目看似基础但要在有限时间内写出高效、健壮的代码并不容易。记得我第一次做求字符串最后一个单词长度这道题时就犯了一个低级错误 - 没有考虑输入字符串末尾有空格的情况。后来通过反复练习总结出这类字符串处理题的通用解法先用trim()去除首尾空格再split()分割最后取数组最后一个元素。这种从错误中积累的经验比单纯背答案要有用得多。2. 字符串处理类高频题精解2.1 最后一个单词长度计算这道题考察的是基础的字符串操作能力。看似简单但有几个容易踩坑的地方字符串可能以空格结尾可能包含连续多个空格整个字符串可能都是空格优化后的代码应该这样写public class LastWordLength { public static void main(String[] args) { Scanner sc new Scanner(System.in); while(sc.hasNext()){ String input sc.nextLine().trim(); if(input.isEmpty()){ System.out.println(0); continue; } String[] words input.split(\\s); System.out.println(words[words.length-1].length()); } } }关键点在于使用trim()先去除首尾空格使用正则表达式\s处理连续空格增加空字符串判断2.2 字符串分割补位这道题要求将字符串按8字符长度分割不足补0。实际业务中类似的需求很常见比如固定长度的报文处理。我推荐的做法是public class StringSplitter { public static void main(String[] args) { Scanner sc new Scanner(System.in); while(sc.hasNext()){ String s sc.nextLine(); splitAndPad(s); } } private static void splitAndPad(String s) { int len s.length(); int start 0; while(start len) { int end Math.min(start 8, len); String segment s.substring(start, end); if(segment.length() 8) { segment String.format(%-8s, segment).replace( , 0); } System.out.println(segment); start 8; } } }这里使用了String.format的格式化功能来补0比字符串拼接更优雅。Math.min避免了数组越界这些都是实际编码中需要注意的细节。3. 数学运算类高频题实战3.1 质数因子分解这道题考察的是对数学概念的理解和编码实现能力。质数因子分解在密码学等领域有重要应用。我最初的做法是暴力遍历后来优化为只检查到√npublic class PrimeFactors { public static void main(String[] args) { Scanner sc new Scanner(System.in); long num sc.nextLong(); if(num 2) return; StringBuilder sb new StringBuilder(); for(int i2; i*inum; i) { while(num%i 0) { sb.append(i).append( ); num / i; } } if(num 1) { sb.append(num).append( ); } System.out.println(sb.toString()); } }关键优化点只需要遍历到√n即可使用StringBuilder提升字符串拼接效率最后要处理剩余的num可能也是质数的情况3.2 进制转换技巧十六进制转十进制是计算机基础但机试时容易忽略大小写字母的处理。我的经验是统一转换为大写处理public class HexConverter { public static void main(String[] args) { Scanner sc new Scanner(System.in); while(sc.hasNext()){ String hex sc.nextLine().substring(2).toUpperCase(); int decimal 0; for(int i0; ihex.length(); i) { char c hex.charAt(i); int digit Character.isDigit(c) ? c-0 : c-A10; decimal decimal*16 digit; } System.out.println(decimal); } } }这里使用了Character.isDigit()方法判断字符类型避免了复杂的条件判断。注意十六进制数通常以0x开头需要substring(2)跳过前缀。4. 集合操作类题目解析4.1 随机数去重排序这道题考察集合的使用技巧。我最初用HashSet去重再转ArrayList排序后来发现TreeSet一步到位更高效public class UniqueSorter { public static void main(String[] args) { Scanner sc new Scanner(System.in); while(sc.hasNext()){ int n sc.nextInt(); TreeSetInteger set new TreeSet(); for(int i0; in; i) { set.add(sc.nextInt()); } set.forEach(System.out::println); } } }TreeSet的优势自动去重自动按自然顺序排序代码更简洁4.2 字符统计技巧统计字符串中某字符出现的次数要注意大小写不敏感的要求。我推荐的做法是public class CharCounter { public static void main(String[] args) { Scanner sc new Scanner(System.in); String str sc.nextLine().toLowerCase(); char target sc.nextLine().toLowerCase().charAt(0); long count str.chars().filter(c - c target).count(); System.out.println(count); } }这里使用了Java 8的Stream API代码更简洁。chars()将字符串转为IntStreamfilter过滤出目标字符count()统计数量。这种函数式编程风格在处理集合类问题时特别高效。5. 算法优化与调试技巧在实际机试中除了写出正确的代码还需要考虑时间复杂度和边界条件。我有几个实用建议先理清思路再编码避免反复修改使用Scanner的hasNext()处理多组输入注意数据范围选择合适的变量类型测试边界条件空输入、极值、特殊字符等合理使用StringBuilder处理字符串拼接比如在质数因子问题中输入可能是很大的数所以要用long而不是int。在字符串处理时要注意各种特殊字符和空值情况。这些细节往往决定了机试的成败。

更多文章