码迷,mamicode.com
首页 > 其他好文 > 详细

几个笔试题目总结

时间:2014-08-30 19:00:09      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   ar   for   div   log   sp   

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 = 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 = 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());
    }

}

以后不断补充。。。

几个笔试题目总结

标签:style   blog   color   java   ar   for   div   log   sp   

原文地址:http://www.cnblogs.com/nannanITeye/p/3946707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!