题目描述
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?
public class Solution {
public int climbStairs(int n) {
int f = 1;
int g = 0;
while(n--!=0)
{
f += g;
g = f - g;
}
return f;
}
}very precise
本文探讨了一个经典的算法问题——爬楼梯。通过递推的方法,实现了计算到达楼梯顶部的不同方式的数量。该算法采用两个变量进行迭代计算,避免了传统递归方法带来的性能问题。
421

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



