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?
class Solution {
public:
int climbStairs(int n) {
if(n <= 0)return 0;
int *step = new int[n + 1];
step[0] = 1;
step[1] = 1;
for(int i = 2; i <= n; i++)
{
step[i] = step[i - 1] + step[i - 2];
}
return step[n];
}
};

本文介绍了一个经典的动态规划问题——爬楼梯问题。该问题要求计算出到达楼梯顶部的不同方式的数量,每次只能上1阶或2阶。通过使用动态规划的方法,文章提供了一种高效的解决方案,并附带了C++实现代码。
483

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



