首页 > 代码库 > 几个笔试题目总结
几个笔试题目总结
1、阿里某个笔试题,两个字符串text,query,找到text中包含的最长的query的字串:
public static String subStr(String text, String query) { if (text != null && query != null) { int length = query.length(); for (int i = 0; i < length; i++) { for (int j = 0; j <= i; j++) { String sub = query.substring(j, length - i + j); if (text.contains(sub)) { return sub; } } } } return null; }
2、阿里某笔试题:找到一个二叉树中最大最小值对应节点的差值的绝对值,也就是节点所在层的差值,并不是本身数值的差值:
package mystudy;import java.util.LinkedList;import java.util.Queue;public class Tree { private TreeNode root; public Tree() { }; public Tree(TreeNode root) { this.root = root; } public void initTree() { root = new TreeNode(8); root.setLeft(new TreeNode(5)); root.getLeft().setLeft(new TreeNode(7)); root.getLeft().setRight(new TreeNode(4)); root.setRight(new TreeNode(9)); root.getRight().setRight(new TreeNode(6)); root.getRight().setLeft(new TreeNode(10)); } public int minMaxSpan() { Queue<TreeNode> queue = new LinkedList<TreeNode>(); TreeNode node; if (root != null) { node = root; int visitedNum = 0, addedNum = 1, levelNum = 1, min, max, depth = 0, minLevel = 0, maxLevel = 0; min = max = root.getValue(); queue.offer(node); while (!queue.isEmpty()) { TreeNode mNode = queue.poll(); if (min > mNode.getValue()) { min = mNode.getValue(); minLevel = depth; } else if (max < mNode.getValue()) { max = mNode.getValue(); maxLevel = depth; } visitedNum++; if (mNode.getLeft() != null) { queue.offer(mNode.getLeft()); addedNum++; } if (mNode.getRight() != null) { queue.offer(mNode.getRight()); addedNum++; } if (visitedNum == levelNum) { depth++; levelNum = addedNum; } } System.out.println("min:" + min + "max:" + max + "minLevel:" + minLevel + "maxLevel:" + maxLevel); return Math.abs(minLevel - maxLevel); } return -1; } public class TreeNode { private TreeNode left; private TreeNode right; private int value; public TreeNode(TreeNode left, TreeNode right, int value) { this.left = left; this.right = right; this.value =http://www.mamicode.com/ value; } public TreeNode(int value) { this(null, null, value); } public TreeNode getLeft() { return left; } public void setLeft(TreeNode left) { this.left = left; } public TreeNode getRight() { return right; } public void setRight(TreeNode right) { this.right = right; } public int getValue() { return value; } public void setValue(int value) { this.value =http://www.mamicode.com/ value; } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Tree tree = new Tree(); tree.initTree(); System.out.println(tree.minMaxSpan()); }}
以后不断补充。。。
几个笔试题目总结
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。