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?
//easy
int climbStairs(int n)
{
if(n <= 0)
{
return 0;
}
if(n <= 2)
{
return n;
}
int first = 1;
int second = 2;
int result = 0;
for(int i = 3; i <= n; i++)
{
result = first + second;
first = second;
second = result;
}
return result;
}
本文探讨了如何使用动态规划解决爬楼梯问题,通过分析每次可以爬1或2级台阶的情况,阐述了一种有效计算到达楼梯顶端的不同方式数量的方法。
840

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



