剑指offer java实现合集(2)第6~10题

6.旋转数组的最小数字

这道题有两道思路,一种是常规的二分查找,也是出题人想要考察的。

用最后一位和中间值比较大小,从而确定当前旋转的状态,然后逐步向下缩小范围。

做法如下:

import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
        int high = array.length-1;
        int low = 0;
        while(low<high){
            int mid = low+(high-low)/2;
            if(array[mid]<array[high]){
                high = mid;
            }else if(array[mid]==array[high]){
                high=high-1;
            }else{
                low = mid+1;
            }
       }
        return array[low];
    }
}

第二种思路是无论这个数组如何旋转,有一点是可以确定的,那就是旋转过后最大的数右边是最小的数(除了旋转了一个完整数组的长度,顺序不变),那么我们只要遍历,找到值一直在增大突然下降,那么下降的位置,就是最小值的位置,如果遍历一遍之后,没有发现这样的值,那么就说明第一个值是最小的值。

import java.util.ArrayList;

public class Solution {
    public int minNumberInRotateArray(int [] array) {
        if(array.length==0){
           return 0;
        }
        int res = -1;//因为所有的值都大于0,如果遍历一遍之后仍未-1.说明第一个数为最小值
        for(int i =0;i<array.length-1;i++){
            if(array[i]>array[i+1]){
                res=array[i+1];
            }
        }
        if(res==-1){
            res = array[0];
        }
        return res;
    }
}

7.斐波那契数列

都说重要,但面试从来不会问系列。。。

public class Solution {
    public int Fibonacci(int n) {
        int [] res = new int [2];
        res[0] = 0;
        res[1] = 1;
        int temp=0;
        
        while(n>0){
            temp= res[0]+res[1];
            res[0]=res[1];
            res[1]=temp;
            n--;
        }
        return res[0];
    }
}

8.跳台阶

也是斐波那契的思路。

非递归:

public class Solution {
    public int JumpFloor(int target) {
        int res[] = new int[2];
        res[0]=1;
        res[1]=1;
        while(target>0){
            int temp = res[0]+res[1];
            res[0] = res[1];
            res[1] = temp;
            target--;
        }
        return res[0];
    }
}

递归时运行时间会长很多。

递归:

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

9.变态跳台阶

//当跳1级台阶时,f(1) = 1;

//当跳2级台阶时,f(2) = f(1) + 1 = 2;

///当跳3级台阶时,f(3) = f(2) + f(1) + 1 = 4;

//当跳4级台阶时,f(4) = f(3) + f(2) + f(1) + 1 = 8;

//。。。。。。。。。。。。。。。。。。。。。

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

10.矩形覆盖

斐波那契变形

public class Solution {
    public int RectCover(int target) {
        int res[] = new int[2];
        if(target==0){
            return 0;
        }
        res[0] = 1;
        res[1] = 1;
        while(target>0){
            int temp = res[0]+res[1];
            res[0]=res[1];
            res[1]=temp;
            target--;
        }
        return res[0];
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值