题目描述
青蛙跳台阶算法,每次可以跳1级或两级,请问有n级台阶,有多少种算法,递归和非递归如何写
代码如下
public class Solution {
public int jumpFloor(int target) {
if(target == 1)return 1;
if(target == 2)return 2;
return jumpFloor(target - 1) + jumpFloor(target - 2);
}
}
变态跳台阶
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶(n为正整数)总共有多少种跳法。
public class Solution {
public int jumpFloorII(int target) {
return (int)Math.pow(2,target-1);
}
}