1.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.
2.题目要求:假设梯子有n层,计算如何爬到第n层,因为每次只能爬1或2步,那么爬到第n层的方法要么是从第n-1层一步上来的,要不就是从n-2层2步上来的,所以递推公式非常容易的就得出了:dp[n] = dp[n-1] + dp[n-2]。
3.方法:斐波那契数列求解。
4.代码:
class Solution {
public:
int climbStairs(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
long long f[100];
f[0] = 1;
f[1] = 1;
for(int i = 2; i < 100; i++)
f[i] = f[i-1] + f[i-2];
return f[n];
}
};