8.跳台阶

题目描述

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

题目解答

public class Solution {
    public int JumpFloor(int target) {
        if(target<=0){
            return 0;
        }
        if(target==1 || target==2){
            return target;
        }
        int res=2;
        int pre=1;
        int tmp=0;
        for(int i=3;i<=target;i++){
            tmp=res;
            res=res+pre;
            pre=tmp;
        }
        return res;
    }
}

动态规划

posted @ 2018-12-14 16:58  chan_ai_chao  阅读(106)  评论(0编辑  收藏  举报