跳台阶

题目描述

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
 
分析:简单的动态规划问题,问题可以简单的考虑子问题的最优解,即f(n)=f(n-1)+f(n-2),通过迭代求得f(n).
public class Solution {
    public int JumpFloor(int target) {
        if(target==0)
            return 0;
        if(target==1)
            return 1;
        if(target==2)
            return 2;
        int t1=1;
        int t2=2;
        int p=target-2;
        int q;
        while(p>0)
        {
            q=t2;
            t2=t1+t2;
            t1=q;
            p--;
        }
        return t2;
        
    }
}
原文地址:https://www.cnblogs.com/leezoey/p/8672610.html