class Solution {
public:
int climbStairs(int n) {
int a = 1, b = 1;
while (-- n) { //往后走n-1步,最后返回的是b,所以是n-1步
int c = a + b;
a = b, b = c;
}
return b;
}
};
class Solution {
public:
int climbStairs(int n) {
vector<int> f(n + 1); //这里必须开成n+1,如果是n的话就越界了
f[0] = 1, f[1] = 1;
for (int i = 2; i <= n; i ++) f[i] = f[i - 1] + f[i - 2];
return f[n];
}
};