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.
Subscribe to see which companies asked this question.
这道题目其实就是斐波那契数列问题,题目比较简单,我们很容易就能列出dp方程
dp[n] = dp[n - 1] + dp[n - 2] 初始条件dp[1] = 1, dp[2] = 2。
代码如下:
class Solution {
public:
int climbStairs(int n) {
int f1 = 2;
int f2 = 1;
if(n == 1) {
return f2;
} else if(n == 2) {
return f1;
}
int fn;
for(int i = 3; i <= n; i++) {
fn = f1 + f2;
f2 = f1;
f1 = fn;
}
return fn;
}
};
本文探讨了一个经典的编程面试题——爬楼梯问题,并通过动态规划的方法给出了一个简洁高效的解决方案。该问题实质上可以归结为斐波那契数列的应用。
318

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



