New Ket Net sword refers to Offer-java

Source: Internet
Author: User

(1) input a binary tree's pre-sequence traversal and the results of the middle sequence traversal, please reconstruct the two-fork tree. Assume that no duplicate numbers are included in the result of the input's pre-order traversal and the middle-order traversal. For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return.

/*** Definition for Binary tree * public class TreeNode {* Int. val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} }*/ Public classSolution { PublicTreeNode Reconstructbinarytree (int[] Pre,int[] in) {        return_reconstructbinarytree (pre, 0, Pre.length-1, in, 0, in.length-1); }    PrivateTreeNode _reconstructbinarytree (int[] Pre,intPreleft,intPreright,int[] in,intInleft,intinright) {        inti;  for(i = inleft; I <= inright; i++) {//Find the root node location            if(Pre[preleft] = =In[i]) {                 Break; }} TreeNode node=NewTreeNode (Pre[preleft]); BooleanLeftflag =true; BooleanRightflag =true; if(i = = Inleft) {//if the number of left dial hand tree nodes is 0, that is, there is no left subtreeLeftflag =false; }        if(i = = inright) {//if the number of right subtree nodes is 0, that is, there is no right sub-treeRightflag =false; }        intLeftnum = I-inleft;//Calculate root node left subtree has several nodes        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); }        returnnode; } }

(2) Use two stacks to implement a queue, complete the queue push and pop operations. The elements in the queue are of type int.

ImportJava.util.Stack; Public classSolution {Stack<Integer> Stack1 =NewStack<integer>(); Stack<Integer> Stack2 =NewStack<integer>();  Public voidPushintnode)    {Stack1.push (node); }         Public intpop () { while(!Stack1.empty ())        {Stack2.push (Stack1.pop ()); }        intnum =Stack2.pop ();  while(!Stack2.empty ())        {Stack1.push (Stack2.pop ()); }        returnnum; }}

(3) Moving a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of a non-descending sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1. Note: All elements given are greater than 0, and if the array size is 0, return 0.

Importjava.util.ArrayList; Public classSolution { Public intMinnumberinrotatearray (int[] Array) {        if(Array.Length = = 0) {            return0; }        inti = 0;  while(I <array.length) {//find the first number less than the previous number            if((i + 1) < Array.Length && Array[i] <= array[i + 1]) {i++; } Else {                 Break; }        }        return(i + 1) < Array.Length? Array[i + 1]: array[0]; }}

(4) A frog can jump up to 1 steps at a time, or jump up to 2 levels. Ask the frog to jump on an n-level step with a total number of hops.

 Public class Solution {    publicint jumpfloor (int  target) {        if ( Target <= 2) {            return  target;        }         return Jumpfloor (target-1) + Jumpfloor (target-2);    }}

A frog can jump up to 1 steps at a time, or jump up to level 2 ... It can also jump on n levels. Ask the frog to jump on an n-level step with a total number of hops.

Import Java.lang.Math;  Public class Solution {    publicint jumpfloorii (int  target) {           return (int) Math.pow (2, Target-1);}    }

(5) Enter an integer that outputs the number of 1 in the binary representation. Where negative numbers are expressed in complement.

 Public classSolution { Public intNUMBEROF1 (intN) {intmask = 1, num = 0;  for(inti = 1; I < 32; i++) {            intT = (int) (Mask &N); if(T > 0) {num++; } Mask<<= 1; }        returnnum; }}

(6) Enter two binary tree, a, B, to determine whether a is a sub-structure. (PS: We agree that an empty tree is not a sub-structure of any tree)

/**Public class TreeNode {int val = 0;    TreeNode left = null;    TreeNode right = null;    Public TreeNode (int val) {this.val = val; }}*/ImportJava.util.Queue; Public classSolution { Public BooleanHassubtree (TreeNode root1,treenode root2) {if(Root2 = =NULL|| ROOT1 = =NULL) {            return false; }         returnIscontain (ROOT1, root2) | | Hassubtree (Root1.left, root2) | |Hassubtree (Root1.right, Root2); }        //determine if the Root2 is a root1 substructure    Private BooleanIscontain (TreeNode root1,treenode root2) {if(Root2 = =NULL) {            return true; }        Else if(ROOT1 = =NULL) {            return false; }        Else {            returnRoot1.val = = Root2.val && iscontain (root1.left, Root2.left) &&Iscontain (Root1.right, root2.right); }    }}

(7)

Find all information for the latest onboarding staff

SELECT * FROM Employees ORDER BY hire_date DESC LIMIT 1;

Find employees with the last employee time ranking third-party all information

SELECT * FROM Employees ORDER BY hire_date DESC limit 2, 1;

New Ket Net sword refers to Offer-java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.