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.
思路:记忆型动规,否则超时
class Solution {
public:
int vis[1000] = {};
int climbStairs(int n) {
if(n == 1)
return 1;
if(n == 2)
return 2;
int adder1 = 0, adder2 = 0;
if(vis[n-1] != 0)
adder1 = vis[n-1];
else
{
adder1 = climbStairs(n-1);
vis[n-1] = adder1;
}
if(vis[n-2] != 0)
adder2 = vis[n-2];
else
{
adder2 = climbStairs(n-2);
vis[n-2] = adder2;
}
return adder1 + adder2;
}
};

本文探讨了一个经典的动态规划问题——爬楼梯问题。给定一个正整数 n,代表需要爬 n 个台阶到达楼梯顶部。每次可以爬 1 或 2 个台阶,求出所有不同的攀爬方式的数量。文章提供了一种使用记忆化搜索的方法来高效地解决这个问题。
305

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



