上台阶 简单动态规划 或者 用循环

来源:互联网 发布:google 财务分析软件 编辑:程序博客网 时间:2024/04/30 11:47

上台阶
  • 热度指数:2859时间限制:3秒空间限制:32768K
  • 本题知识点: 递归 动态规划
  •  算法知识视频讲解

题目描述

有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或者二级,要走上m级,共有多少走法?注:规定从一级到一级有0种走法。

给定一个正整数int n,请返回一个数,代表上楼的方式数。保证n小于等于100。为了防止溢出,请返回结果Mod 1000000007的值。

测试样例:
3
返回:2
import java.util.*;public class GoUpstairs {        int[] count = new int[100];    public int countWays(int n) {    for(int i=0;i<100;i++){            count[i]=-1;        }        return cal(n);    }    public int cal(int n){        if(count[n]!=-1)return count[n];        if(n<=3) return n-1;        else {            count[n]=(cal(n-1) + cal(n-2))%1000000007;            return count[n];        }    }}







0 0
原创粉丝点击