牛客网剑指offer刷题Java版-8跳台阶

题目描述
一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果)

整体和费纳波切类似

递归:

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

迭代:

public class Solution {
    public int JumpFloor(int target) {
        if (target==0)
            return 1;
        if (target==1)
            return 1;
        int x=1;
        int y=1;
        while(target>1){
            y=x+y;
            x=y-x;
            target--;
        }
        return y;
    }
}
发布了19 篇原创文章 · 获赞 0 · 访问量 199

猜你喜欢

转载自blog.csdn.net/qq_42632671/article/details/104261976