经典指数          
原因
3298
浏览数
0
收藏数
 

有一楼梯共m级,刚开始时你在第一级,若每次只能跨上一级或者二级,要走上m级,共有多少走法?注:规定从一级到一级有0种走法。 给定一个正整数int n,请返回一个数,代表上楼的方式数。保证n小于等于100。为了防止溢出,请返回结果Mod 1000000007的值。 测试样例: 3 返回:2

     举报   纠错  
 
切换
1 个答案
这题用递归的斐波那契数列算法会超时: public int countWays(int n) { // write code here if(n==0){ return 0; }else if(n==1){ return 1; }else if(n==2){ return 2; }else{ return countWays(n-1)+countWays(n-2); } } 所以可以把他改成动态规划: public int countWays(int n) { // write code here int dp[]=new int [n]; dp[0]=0; dp[1]=1; dp[2]=2; if(n>2){ for(int i=3;i
 
切换
撰写答案
扫描后移动端查看本题