携程2019校园秋招后台开发笔试题(Java)
时间:20180904 19:00~20:30
分两部分:第一部分单选题20道,第二部分编程题3道。直接上编程。
一、bitcount
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long num = 0; while(n != 0){ num++; n = n & (n-1); } //num = Long.bitCount(n); System.out.println(num); } }
也可以直接调用Long类的bitcount,以前不知道这个方法,受教了。
二、查询满足区间的记录
大家都说这个题出的水了,下面的解法时间复杂度是O(n),如果有大佬有O(logn)解法,欢迎留言。
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int target = sc.nextInt(); List<Record> list = new ArrayList<Record>(); for (int i = 0; i < num; i++) { list.add(new Record(sc.nextInt(), sc.nextInt(), sc.nextInt())); } List<Integer> recordList = new ArrayList<Integer>(); for (int i = 0; i < num; i++) { if (target >= list.get(i).begin && target <= list.get(i).end) recordList.add(list.get(i).number); } if (recordList.isEmpty()) System.out.println("null"); else { Collections.sort(recordList); for (Integer record : recordList) System.out.println(record); } sc.close(); } } class Record { int number; int begin; int end; public Record(int number, int begin, int end) { super(); this.number = number; this.begin = begin; this.end = end; } }
三、LRU Cache
LeetCode上面的题目LeetCode146,然而我并没有做过,还是刷题刷的少啊!泪奔!!!
LRU缓存利用了这样的一种思想。LRU是Least Recently Used 的缩写,翻译过来就是“最近最少使用”,也就是说,LRU缓存把最近最少使用的数据移除,让给最新读取的数据。而往往最常读取的,也是读取次数最多的,所以,利用LRU缓存,我们能够提高系统的performance。
代码可参考:,写得很详细,我就偷个懒啦!!!
上一篇:
Java基础知识总结(2021版)
下一篇:
大数据面试题:介绍下Flume