题型
在牛客网笔试,编码是核心代码模式。
- 10道单选
- 5道多选
- 2道编程
单选
考了Java基础、数据结构与算法基础、数据库索引等。
印象比较深的:
- Java种面向字符的输入流
- 排序算法的时间复杂度
- 索引设计:有两条SQL语句:select * from table where a=1 与 select * from table where b=1 and a=1,索引应该如何设计
- 哈夫曼树的带权路径长度:忘了咋算了
多选
印象比较深的:
- Java中基于链表的容器有?
- 分治法的前提条件
编程
密码破译问题:字母A-Z对应数字1-26,输入一个数字,输出有几种解码方法。
我用了回溯法,这题感觉是分割字符串的类型,我的解法超时了。本地代码如下:import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class youzan01 { private static List<List<String>> res = new ArrayList<>(); private static LinkedList<String> path = new LinkedList<>(); public static void main(String[] args) { String s = "226"; backtracking(s, 0); // 打印分割字符串结果 print(); } // 回溯函数 public static void backtracking(String s, int start) { // 终止条件 if (start >= s.length()) { res.add(new ArrayList<>(path)); return; } // 单层搜索逻辑 for (int i = start; i < s.length(); i++) { // 分割的字符串 String str = s.substring(start, i + 1); // 分割的字符串长度大于2 if(str.length() > 2){ continue; } // 分割的字符串大于26或开头为0 long num = Long.parseLong(str); if (num > 26 || s.charAt(start) - '0' < 1) { continue; } path.add(str); backtracking(s, i + 1); path.removeLast(); } } // 打印结果 public static void print(){ for (int i = 0; i < res.size(); i++) { System.out.println(); for (int j = 0; j < res.get(i).size(); j++) { System.out.print(res.get(i).get(j) + " "); } } System.out.println(); System.out.println("共有" + res.size() + "种解码方法"); } }
自测数据为20个1的话也通过了,结果是10946种。但是提交的时候不知道哪个测试样例没AC,希望有老哥AC了让我参考一下。
判断回文数字:输入一个数字,判断是不是回文数字
数字转换成字符串后,用双指针判断。AC了,本地代码如下:public class youzan02 { public static void main(String[] args) { int num = 123432; String num2 = Integer.toString(num); int left = 0, right = num2.length()-1; while(left <= right){ if(num2.charAt(left++) != num2.charAt(right--)){ System.out.println("false"); } } System.out.println("true"); } }
如果有老哥有其他解法的话希望能留言一下。
感想
笔试这么多场,终于有用核心代码模式编程的了。lz是个弱鸡,也就在LeetCode上刷了两三个月的题,没怎么做过ACM的题目,自己写输入输出也经常出错,难得做一场核心代码模式的笔试,不用自己写输入输出真的是热泪盈眶。
希望有老哥指点一下怎么快速掌握ACM模式下的输入输出。
全部评论
(2) 回帖