牛客網劍指offer-Java

來源:互聯網
上載者:User

標籤:nod   最小值   mask   min   樹節點   輸入   最小   limit   結構   

(1)輸入某二叉樹的前序走訪和中序遍曆的結果,請重建出該二叉樹。假設輸入的前序走訪和中序遍曆的結果中都不含重複的數字。例如輸入前序走訪序列{1,2,4,7,3,5,6,8}和中序遍曆序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。

/** * Definition for binary tree * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {   public TreeNode reConstructBinaryTree(int [] pre,int [] in) {        return _reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);    }    private TreeNode _reConstructBinaryTree(int [] pre, int preLeft, int preRight, int [] in, int inLeft, int inRight) {        int i;        for (i = inLeft; i <= inRight; i++) { //找到根節點所在位置            if (pre[preLeft] == in[i]) {                break;            }        }        TreeNode node = new TreeNode(pre[preLeft]);         boolean leftFlag = true;        boolean rightFlag = true;        if (i == inLeft) { //如果左子樹節點數為0,也就是沒有左子樹            leftFlag = false;        }        if (i == inRight) { //如果右子樹節點數為0,也就是沒有右子樹            rightFlag = false;        }        int leftNum = i - inLeft; //計算根節點左子樹有幾個節點        if (leftFlag) {            //System.out.println((preLeft + 1) + " " + (preLeft + leftNum) + " " + inLeft + " " + (i - 1));            node.left = this._reConstructBinaryTree(pre, preLeft + 1, preLeft + leftNum, in, inLeft, i - 1);        }        if (rightFlag) {            //System.out.println((preLeft + leftNum + 1) + " " + (preRight) + " " + (i + 1) + " " + (inRight));            node.right = this._reConstructBinaryTree(pre, preLeft + leftNum + 1, preRight, in, i + 1, inRight);        }        return node;    } }

(2)用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素為int類型。

import java.util.Stack;public class Solution {    Stack<Integer> stack1 = new Stack<Integer>();    Stack<Integer> stack2 = new Stack<Integer>();        public void push(int node) {         stack1.push(node);    }        public int pop() {        while (!stack1.empty()) {            stack2.push(stack1.pop());        }        int num = stack2.pop();        while (!stack2.empty()) {            stack1.push(stack2.pop());        }        return num;    }}

(3)把一個數組最開始的若干個元素搬到數組的末尾,我們稱之為數組的旋轉。 輸入一個非遞減排序的數組的一個旋轉,輸出旋轉數組的最小元素。 例如數組{3,4,5,1,2}為{1,2,3,4,5}的一個旋轉,該數組的最小值為1。 NOTE:給出的所有元素都大於0,若數組大小為0,請返回0。

import java.util.ArrayList;public class Solution {    public int minNumberInRotateArray(int [] array) {        if (array.length == 0) {            return 0;        }        int i = 0;        while (i < array.length) {            //找到第一個小於前一個數的數            if ((i + 1) < array.length && array[i] <= array[i + 1]) {                i++;            } else {                break;            }        }        return  (i + 1) < array.length ? array[i + 1] : array[0];           }}

(4)一隻青蛙一次可以跳上1級台階,也可以跳上2級。求該青蛙跳上一個n級的台階總共有多少種跳法。

public class Solution {    public int JumpFloor(int target) {        if (target <= 2) {            return target;        }        return JumpFloor(target - 1) + JumpFloor(target - 2);    }}

一隻青蛙一次可以跳上1級台階,也可以跳上2級……它也可以跳上n級。求該青蛙跳上一個n級的台階總共有多少種跳法。

import java.lang.Math;public class Solution {    public int JumpFloorII(int target) {           return (int)Math.pow(2, target - 1);    }}

(5)輸入一個整數,輸出該數二進位表示中1的個數。其中負數用補碼錶示。

public class Solution {    public int NumberOf1(int n) {        int mask = 1, num = 0;        for (int i = 1; i < 32; i++) {            int t = (int)(mask & n);            if (t > 0) {                num++;            }            mask <<= 1;        }        return num;    }}

(6)輸入兩棵二叉樹A,B,判斷B是不是A的子結構。(ps:我們約定空樹不是任意一個樹的子結構)

/**public class TreeNode {    int val = 0;    TreeNode left = null;    TreeNode right = null;    public TreeNode(int val) {        this.val = val;    }}*/import java.util.Queue;public class Solution {    public boolean HasSubtree(TreeNode root1,TreeNode root2) {        if (root2 == null || root1 == null) {            return false;        }         return IsContain(root1, root2) || HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);       }        //判斷root2是否是root1的子結構    private boolean IsContain(TreeNode root1,TreeNode root2) {        if (root2 == null) {            return true;        }        else if (root1 == null) {            return false;        }        else {            return root1.val == root2.val && IsContain(root1.left, root2.left) && IsContain(root1.right, root2.right);        }    }}

(7)

尋找最晚入職員工的所有資訊

select * from employees order by hire_date desc limit 1;

尋找入職員工時間排名倒數第三的員工所有資訊

select * from employees order by hire_date desc limit 2,1;

 

牛客網劍指offer-Java

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.