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?
题意:n阶台阶,每次爬1阶或2阶,问有多少种爬法。
思路:动态规划:f(n) = f(n-1)+f(n-2) (即走一步还是走两步到达n)。
class Solution {
public:
int climbStairs(int n) {
if(0 == n)
return 0;
vector<int> f(n+1, 0);
f[1] = 1;
f[2] = 2;
for(int i=3;i<=n;i++)
f[i] = f[i-1]+f[i-2];
return f[n];
}
};