关于java:JZ008跳台阶

8次阅读

共计 431 个字符,预计需要花费 2 分钟才能阅读完成。

跳台阶

题目形容

一只青蛙一次能够跳上 1 级台阶,也能够跳上 2 级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后秩序不同算不同的后果)

题目链接 : 跳台阶

代码

public class Jz08 {

    /**
     * 迭代法
     *
     * @param target
     * @return
     */
    public static int jumpFloor(int target) {if (target <= 2) {return target;}
        int last1 = 1, last2 = 2, result = last1 + last2;
        for (int i = 3; i <= target; i++) {
            result = last1 + last2;
            last1 = last2;
            last2 = result;
        }
        return result;
    }

    public static void main(String[] args) {for (int i = 1; i < 10; i++) {System.out.println(jumpFloor(i));
        }
    }
}

【每日寄语】你的微笑是最有治愈力的力量,胜过世间最美的风光。

正文完
 0