爬楼梯
思路:为了爬到n层楼梯,有两个选择,在n-1阶前进1步 or 在 n-1阶前进2步,即f(n) = f(n-1) + f(n-2)
class Solution {
public:
int climbStairs(int n) {
int prev = 0;
int cur = 1;
for(int i=1; i<=n; i++)
{
int tmp = cur;
cur += prev;
prev = tmp;
}
return cur;
}
};