4.重建二叉树
题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
思路:
前序遍历的第一个节点是根节点。
这样可以根据中序遍历中根节点的位置将二叉树分成两个字树(左子树、右子树)递归
代码:
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1);
return root;
}
//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
if(startPre>endPre||startIn>endIn)
return null;
TreeNode root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;i++)
if(in[i]==pre[startPre]){
root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
break;
}
return root;
}
5.两个栈实现队列
题目:
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
思路:
队列的特点是:先进先出
栈的特点:后进先出
s1用于入队;s2用于出队
- 若s1,s2都为空:已经没有数据可以出队
- 若s2有值,直接弹出栈顶即可(s1后进的先出到s2,s2中先出的一定是s1中后进的对应队列先进先出)
- 若s2为空,s1弹出栈顶元素到s2,然后 从s2中弹出(用while将s1中所有元素一次性投入s2的原因是:在连续从队列中移除数据时,不需要频繁的将数据在两个栈之间导来导去)
代码:
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
public void push(int node) {
//入队操作就是简单的入栈
s1.push(node);
}
public int pop() {
if(s1.isEmpty()&&s2.isEmpty())
throw new RuntimeException("Queue is empty!");
if(s2.isEmpty()){
//当s1不为空的时候 ,弹出所有的元素
while(!s1.isEmpty())
s2.push(s1.pop());
}
return s2.pop();
}
5.1两个队列实现一个栈
题目: 用两个队列来实现一个栈,完成栈的Push和Pop操作。 栈中的元素为int类型。
思路:
代码 :
经典面试题:两个栈实现队列+两个队列实现栈----java:
https://blog.csdn.net/u011328417/article/details/79436120
6.旋转数组的最小数字
题目:
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
思路:
采用二分法解答这个问题 mid = low + (high - low)/2
需要考虑三种情况:
- array[mid] > array[high]:
出现这种情况的array类似[3,4,5,6,0,1,2],此时最小数字一定在mid的右边→ low = mid + 1 - array[mid] == array[high] :
出现这种情况的array类似 [1,0,1,1,1] 或者[1,1,1,0,1],此时最小数字不好判断在mid左边,还是右边,这时只好一个一个试 ,所以从高到低找最小值→high = high - 1 - array[mid] < array[high]:
出现这种情况的array类似[2,2,3,4,5,6,6],此时最小数字一定就是array[mid]或者在mid的左边。因为右边必然都是递增的。→high = mid
注意这里有个坑:如果待查询的范围最后只剩两个数,那么mid 一定会指向下标靠前的数字:
比如 array = [4,6] array[low] = 4 ;array[mid] = 4 ; array[high] = 6 ;
如果high = mid - 1,就会产生错误, 因此high = mid 但情形(1)中low = mid + 1就不会错误
代码:
import java.util.ArrayList;
public class Solution {
public int minNumberInRotateArray(int [] array) {
if(array.length==0)
return 0 ;
int low =0,high = array.length-1;
//利用二分法寻找最小值
while(low<high){
int mid = low + (high-low)/2;
//如果中值大于high,那么最小值必定在mid+1,high之间(由于本题数据有序)
if(array[mid]>array[high])
low = mid + 1;
//如果中值等于high,说明最小值在mid,high之间
else if(array[mid]==array[high])
high = high -1 ;
//如果中间值小于high,那么最小值可能是mid也可能是比mid还要小的数
else
high = mid;
}
return array[low];
}
}
7.斐波那契额数列
题目:
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0)。 n<=39
思路:
1.递归法(慢,效率低 ,简单): F[N]=F[N-1]+F[N-2];
缺点:每次计算Fibonnacci(i)都要将2~(i-1)都算一遍,如果n很大,递归花费时间很长,时间复杂度随着n的指数方式增大
return Fibonnacci(n-1)+Fibonnacci(n-2);
2.循环法:
计算0~(n-1)的值并充分利用,复杂度O(n)
代码:
//循环法1:通过数组来暂存
public class Solution {
public int Fibonacci(int n) {
if(n<=1)
return n;
int res[] = new int[n+1];
res[0]=0;
res[1]=1;
for(int i=2;i<=n;++i)
res[i] = res[i-2]+res[i-1];
return res[n];
}
}
//循环法2:通过变量暂存并替换
public class Solution {
public int Fibonacci(int n) {
if(n<2)
return n;
int fbNMinusOne = 1,fbNMinusTwo=0;
int fbN = 0;
for(int i=2 ; i<=n ; ++i){
fbN = fbNMinusOne + fbNMinusTwo;
//此处注意应该先改变i-2再改变i-1,否则会错(坑)
fbNMinusTwo = fbNMinusOne;
fbNMinusOne = fbN;
}
return fbN;
}
}
8.青蛙跳台阶
题目:
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)。
思路:
斐波那契额数列的变形(找到规律即可)
代码:
public class Solution {
public int JumpFloor(int target) {
if(target<=2)
return target;
int result[]=new int[target+1];
result[0]=0;
result[1]=1;
result[2]=2;
for(int i=3;i<=target;i++) {
result [i]=result[i-1]+result[i-2];
}
return result[target];
}
}
9.变态跳台阶
题目:
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
思路:
斐波那契延伸
因为n级台阶,第一步有n种跳法:跳1级、跳2级、到跳n级
跳1级,剩下n-1级,则剩下跳法是f(n-1)
跳2级,剩下n-2级,则剩下跳法是f(n-2)
所以f(n)=f(n-1)+f(n-2)+…+f(1)
构建一个数列,分别存储f(1)-f(n-1),f(n)为前者之和
代码:
public class Solution {
public int JumpFloorII(int target) {
int[] result= new int[target+1];
result[0]=1;
for(int i=1;i<=target;i++) {
for(int j=0;j<i;j++)
result[i]+=result[j];
}
return result[target];
}
}
改进:
f(n)=f(n-1)+f(n-2)+…+f(1)=2*f(n-1);(因为f(n-1) = f(n-2)+f(n-3)+…+f(1));
public class Solution {
public int JumpFloorII(int target) {
if(target<=1)
return target;
int[] result= new int[target+1];
result[1]=1;
for(int i=2;i<=target;i++) {
result[i]= 2 * result[i-1];
}
return result[target];
}
}
10.矩形覆盖
题目:
我们可以用21的小矩形横着或者竖着去覆盖更大的矩形。请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
思路:
斐波那契延伸
代码:
public class Solution {
public int RectCover(int target) {
int result = 0;
int tmins1=2;
int tmins2=1;
if(target<=2)
return target;
for(int i=3;i<=target;i++){
result = tmins1+tmins2;
tmins2=tmins1;
tmins1=result;
}
return result;
}
}