zoukankan      html  css  js  c++  java
  • 变态跳台阶

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

     设f(n) 为n级台阶的跳法  且f(0) = 1,然后分别针对n的取值进行讨论,由第一步的跳法不同进行分情况讨论,从而构造出递归关系等式。本题的关键就是要构造出递归关系式

     resolution1:自底向上

     public int JumpFloorII(int target) {
               int[] countArray = new int[target+1];
            countArray[0] = 1;
            countArray[1] = 1;
            for(int i = 2;i <= target;i++){
                countArray[i] = 2 * countArray[i-1];
            }
            return countArray[target];
        }

    resolution2:自顶向下

     public int JumpFloorII(int target) {
               int result = 0;
            if(target == 1) return 1;
            if(target >= 2) result = 2 * JumpFloorII(target - 1);
            return result;
        }
    欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
  • 相关阅读:
    Python
    Python
    Python
    Python
    Python
    Python
    Scala核心编程_第01章_Scala概述
    与富婆讨论性,死亡与生活的意义
    python邮件发送给多人时,只有第一个人能收到的问题
    少年维特的烦恼
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10326841.html
Copyright © 2011-2022 走看看