一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
分析思路:
当n=1时,有一种跳法;当n=2时,有两种跳法;当n=3时,为前两中跳法之和,以此类推......可知:这与斐波那契数列类似。
非递归方法求解:
public class Solution {
public int JumpFloor(int target) {
if (target<=2)
{
return target;
}
int first=1,second=2,total=0;
for(int i=3;i<=target;i++)
{
total=first+second;
first=second;
second=total;
}
return total;
}
}