You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
解法1:记忆化数组
class Solution {
public:
int value[1000]={};
int climbStairs(int n) {
if(n==1)return 1;
else if(n==2)return 2;
else{
if(value[n-1]!=0){
int k=value[n-1];
int s=k+climbStairs(n-2);
value[n]=s;
return s;
}
else{
int s=climbStairs(n-1)+climbStairs(n-2);
value[n]=s;
return s;
}
}
}
};
本文探讨了经典的爬楼梯问题,即如何用不同方式爬到楼梯顶部。通过使用记忆化数组的方法来优化递归解决方案,避免重复计算,提高算法效率。

724

被折叠的 条评论
为什么被折叠?



