剑指-简单-青蛙跳台阶(不同于斐波)

题目描述

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

思路

台阶数 跳法
1 1
2 2
3 4
4 8
5 16
6 32

在这里插入图片描述
简单明了,区别于只能挑1,2级台阶的跳法(此法为斐波那契),递归即可得答案。

代码

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

猜你喜欢

转载自blog.csdn.net/qq_32301683/article/details/108395768