JZ-008-跳台阶

跳台阶

题目描述

一只青蛙一次可以跳上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));
        }
    }
}

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

原文地址:https://www.cnblogs.com/kaesar/p/15474201.html