offer

来源:互联网 发布:域名注册网站哪个好 编辑:程序博客网 时间:2024/05/21 19:04

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) {
        if (pre == null || in == null) {
        return null;
       }
       return ConstructCore(pre, 0, pre.length -1, in, 0, in.length -1);
        
    }
    public TreeNode ConstructCore(int[] pre, int startPre, int endPre, int[] in, int startIn, int endIn) {
int rootValue = pre[startPre];
TreeNode root = new TreeNode(rootValue);
root.left = null;
root.right = null;

if (startPre == endPre && startIn == endIn) {
return root;
}

int leftLen = 0;
int rootIn = startIn;
while (startIn <= endIn && in[startIn] != rootValue) {
startIn++;
leftLen++;
}
int leftPreEnd = startPre + leftLen;
if (leftLen > 0) {
// 构建左子树
root.left = ConstructCore(pre, startPre + 1, leftPreEnd, in, rootIn, startIn - 1);
}
if (leftLen < endPre - startPre) {
//构建右子树
root.right = ConstructCore(pre, leftPreEnd + 1, endPre, in, startIn + 1, endIn);
}
return root;
}
}


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() {
    Integer re=null; 
      if(!stack2.empty()){  // 如果栈2不是空的,那么把最上面那个取出来
          re=stack2.pop(); 
      }else{ 
              //如果栈2是空的,就把栈1里的数一个个取出来,放到栈2里
          while(!stack1.empty()){   
               re=stack1.pop(); 
               stack2.push(re); 
                            } 
                 //栈2里有数之后,再次把里面的数取出来
                 if(!stack2.empty()){ 
                        re=stack2.pop(); 
                  } 
      } 
      return re; 
    }
}


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 -1;
   }//if
   int start = 0,end = array.length-1;
   // 数组有序
   if(array[end] > array[start]){
       return array[start];
   }//if
   // 数组旋转
   // 二分查找
   while(start <= end){
       int mid = (start + end) / 2;
       // [start,mid]有序[mid,end]无序
       if(array[mid] > array[start]){
           start = mid;
       }
       // [start,mid]无序[mid,end]有序
       else if(array[mid] < array[start]){
           end = mid;
       }
       else{
           return array[mid+1];
       }
   }//while 
return array[start]; 
   
    }
}

4/斐波纳妾数列

大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项。

n<=39

public class Solution {
    public int Fibonacci(int n) {
if(n == 0)
            return 0;
        if(n == 1)
            return 1;
        int numfn1 = 0, numfn2 = 1;
        int currentnum= 0;
        for(int i=2; i<=n; ++i) {
            currentnum = numfn1+numfn2;
            numfn1 = numfn2;
            numfn2 = currentnum;
        }
        return currentnum;
    }

}

8.跳台阶

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

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

9/变态跳台阶

一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。

public class Solution {
    public int JumpFloorII(int target) {
        if(target<=0)
            return 0;
        if(target==1)
            return 1;
        else
            return 2 * JumpFloorII(target-1);
    }
}

10/矩形覆盖

我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

public class Solution {
    public int RectCover(int target) {
int res=0;
        int pre1=1;
         int pre2=2;
         if(target==0) return 0;
        if(target==1) return 1;
          if(target==2) return 2;
          for(int i=3;i<target+1;i++){
             res=pre1+pre2;
             pre1=pre2;
             pre2=res;
        }
         return res;
    }
}


public class Solution {
    public int RectCover(int number) {
if(number==0)
            return 0;
        else if(number==1)
            return 1;
        else if(number==2)
            return 2;
        else 
        return RectCover(number-1)+RectCover(number-2);
        
    }
}

0 0