再来五道剑指offer题目

6、旋转数组的最小数字

解题思路:分治递归,直到出现一个递增的序列后停止。

import java.util.*;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if(array.length==0) return 0;
        if(array[0] < array[array.length-1]||array.length==1) return array[0];
        if(array.length==2) return array[1];
        if(array[array.length/2]>array[0]) {
            return minNumberInRotateArray(Arrays.copyOfRange(array,array.length/2+1,array.length));
        }else{
            return minNumberInRotateArray(Arrays.copyOfRange(array,0,array.length/2+1));
        }
    }
}

7、斐波那契数列

解题思路:递归太慢,只能迭代。

public int Fibonacci(int n) {
        if(n==0) return 0;
        int res = 1,tmp=1,tmp2=1;
        for(int i=2;i<n;i++) {
            res=tmp+tmp2;
            tmp2=tmp;
            tmp=res;
        }
        return res;
    }

8、跳台阶

解题思路:青蛙每次只有俩种结果。跳一次+1,跳俩阶+2

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

9、变态跳台阶

解题思路:f(n) = 2^(n-1)

public class Solution {
    public int JumpFloorII(int target) {
        return 1<<(target-1);
    }
}

10、矩形覆盖

解题思路:递归

public class Solution {
    public int RectCover(int target) {
        if(target <=2){
            return target;
        }else{
            return RectCover(target-1) + RectCover(target-2);
        }
    }
}
02-11 00:07