一、给定一个整数序列,请获取最小(最大)的K个数字
这个题还算比较熟悉,在大数据计算时,经常会遇到类似于“Top N”的情况,这个题的解法有很多种,本例还是采用惯例做法:创建一个K大小的容器,容器内的数字都是排序的;在遍历输入序列时,如果遇到比容器内最大值还要大的数字时,则将容器中的最小值移除,即容器中保留K个“已经遇见”的最大数字。在java中,我们直接使用TreeSet作为容器,当然你也可以自己构建一个红黑数。
代码样例:
public static TreeSet<Integer> find(int[] source,int k) { TreeSet<Integer> top = new TreeSet<>(); for(int i= 0; i< source.length; i++) { int item = source[i]; if(top.isEmpty() || top.size() < k) { top.add(item); continue; } if(top.last() > item) { top.add(item); top.pollLast();//移除最后一个 } } return top; }
二、给定一个整数数组序列,请计算出“连续子数组”和值的最大值,数组中正负值都有,无序
描述:输入一个数组,这个数组中有正、负整数组成,求数组中“连续的几个元素”的和值的最大值;比如原始数组为{1,-2,3,10,-4,7,2,-5},其中{3,10,-4,7,2}这个子数组的和值最大,为18。
这个题其实并不复杂,认识到判断时机就可以突破了。
数组:1,-2,3,10,-4,7,2,-5 1) 初始最大值为,max = 1,即数组的第一个元素。 2) 1,-2和值为-1,因为-1比max小,继续。 3) -1,3和值为-2,此时我们就需要做中断了,因为-2比当前元素3的值还要小, 因此我们可以断定,前面的“子数组”的计算是无效的 (只要它参与计算,只会降低总和); 所以此时,我们将3作为新子数组的开始,max = 3,继续遍历。 4) 遍历时,如果和值比当前max大,则替换max。 直到数组的结尾!
代码样例:
private static int maxSum(int[] source) { if(source.length == 0) { throw new RuntimeException("Empty array!"); } int max = source[0]; int length = source.length; int current = max; for (int i = 1 ; i < length; i++) { current = source[i] + current; if (current <= source[i]) { max = source[i]; current = max; continue; } if(current > max) { max = current; } } return max; }
三、给定一个字符数组,请获得此数组中重复出现次数最多的字符元素(字符元素一定有重复)
这个题,其实很简单,如果你钻了牛角尖,估计就比较麻烦:比如你尝试使用逐个遍历比较的方式!我们已知的字符是有限的,我们可以使用Map来保存每个字符以及其出现的次数,这样就很容易得到答案,Map的key为字符值,value为其出现的次数。
代码样例:
public static char maxTimes(char[] sources) { if(sources.length == 0) { throw new RuntimeException("Empty array!"); } int maxTimes = 1; char result = sources[0]; Map<Character,Integer> container = new HashMap<>(); for(int i = 0;i < sources.length; i++) { char item = sources[i]; if(container.containsKey(item)) { Integer times = container.get(item); times++; if(times > maxTimes) { maxTimes = times; result = item; } container.put(item,times++); }else { container.put(item,1); } } return result; }
题目的变种可以为:
给定1亿个数字,它们被保存在文件中,请你找出出现次数最多的哪个数字!
思路为分治法,将数组取模并分散存储在多个小文件中,确保相同的数字被转存在相同的文件中,然后逐个计算每个文件中出现次数最多的数字,然后取次数最多的那个即可。
四、有两个单向链表,它们在某个节点开始,后续节点都重合,请你用程序找出首个重合点。
比如如下两个链表:
1-->2-->3-->4-->5-->6-->7
7-->10->5-->6-->7
我们看出,5是这两个链表的首个重合点。要实现这个算法,用粗暴的方式逐个遍历复杂度太高,当然实现起来简单。我们在上述几个题目都已经表示,对于单向链表,从后遍历或者查找,最好的办法就是借助辅助栈,这个题目我们也这么做。
用两个辅助栈分表保存链表的节点,然后同时逐个弹出,并比较栈顶的值,直到第一个不相同的节点出现为止。代码样例如下:
public static Node joinPoint(LinkedNodes first,LinkedNodes second) { if(first == null || second == null) { return null; } Node firstRoot = first.header.next; Node secondRoot = second.header.next; if(firstRoot == null || secondRoot == null) { return null; } //借助辅助栈,将单向链表反序 Stack<Node> data1 = new Stack<>(); Stack<Node> data2 = new Stack<>(); while (true) { Node item = firstRoot.next; if(item == null) { break; } data1.push(item); } while (true) { Node item = secondRoot.next; if(item == null) { break; } data2.push(item); } //依次弹出栈,并比较,最后一个相同点就是join点 Node result = null; while (true) { Node n1 = data1.pop(); Node n2 = data2.pop(); if(n1.value != n2.value) { break; }else { result = n1;//or n2; } } return result; }
五、求一个二叉树的最大深度
二叉树的树深,就是root节点到最底层叶子节点的最大距离。很简单,递归实现;代码样例如下:
public static int depth(BinaryTreeNode node) { if(node == null) { return 0; } BinaryTreeNode left = node.left; int l = 0; if(left != null) { l = depth(left); } BinaryTreeNode right = node.right; int r = 0; if(right != null) { r = depth(right); } return l > r ? l + 1 : r + 1; }