Fibonacci青蛙跳台阶

欢迎关注作者博客
简书传送门

/**
 * @program:
 * @description:
 * @author: zhouzhixiang
 * @create: 2018-10-31 19:38
 */
public class Fibonacci {

    public static void main(String[] args) throws Exception {
        getStepMain(30);
        getStepMain2(30);
    }

    /** 
     * @Description: 递归获取
     * @Param:  
     * @return:  
     * @Author: zhouzhixiang
     * @Date: 2018/10/31 
     */
    public static void getStepMain(int n) throws Exception {
        System.out.println("递归。。。。。。。。。。。。。");
        //开始时的纳秒
        long start = System.nanoTime();
        long step = getStep1(n);
        //花费的纳秒数
        long during = System.nanoTime() - start;
        System.out.println(during);
        //将纳秒转换为毫秒
        long seconds = TimeUnit.MILLISECONDS.convert(during, TimeUnit.NANOSECONDS);
        System.out.println(step);
        System.out.println("当n为" + n + "时,花费时间为" + seconds + "毫秒");
    }

    /**
     * @Description: 递归获取
     * f1=1,f2=2,f3=f1+f2......fn=fn-1+fn-2
     * @Param:
     * @return:
     * @Author: zhouzhixiang
     * @Date: 2018/10/31
     */
    public static long getStep1(int n) throws Exception {
        if(n<=0) throw new Exception("台阶数量不符合要求");
        if(n==1) return 1;
        if(n==2) return 2;
        return getStep1(n-1)+getStep1(n-2);
    }

    /**
     * @Description: 非递归获取
     * @Param:
     * @return:
     * @Author: zhouzhixiang
     * @Date: 2018/10/31
     */
    public static void getStepMain2(int n) throws Exception {
        System.out.println("非递归。。。。。。。。。。。。。");
        //开始时的纳秒
        long start = System.nanoTime();
        long step = getStep2(n);
        //花费的纳秒数
        long during = System.nanoTime() - start;
        System.out.println(during);
        //将纳秒转换为毫秒
        long seconds = TimeUnit.MILLISECONDS.convert(during, TimeUnit.NANOSECONDS);
        System.out.println(step);
        System.out.println("当n为" + n + "时,花费时间为" + seconds + "毫秒");
    }

    /**
     * @Description: 递归获取
     * f1=1,f2=2,f3=f1+f2......fn=fn-1+fn-2
     * @Param:
     * @return:
     * @Author: zhouzhixiang
     * @Date: 2018/10/31
     */
    public static long getStep2(int n) throws Exception {
        if(n<=0) throw new Exception("台阶数量不符合要求");
        if(n==1) return 1;
        if(n==2) return 2;
        int one = 1;
        int two = 2;
        int result = 0;
        for(int i = 3; i < n; i++){
            result = one + two;
            one = two;
            two = result;
        }
        return result;
    }

}

剑指offer之Fibonacci青蛙跳台阶

相关文章:

  • 2022-01-23
  • 2022-01-17
  • 2021-03-30
  • 2021-10-26
  • 2021-10-04
  • 2021-12-29
  • 2021-09-21
  • 2022-12-23
猜你喜欢
  • 2021-03-29
  • 2022-12-23
  • 2021-06-25
  • 2022-12-23
相关资源
相似解决方案