DP写法:
public int climbStairs(int n) {
if(n == 0 || n == 1)
return n;
int [] dp = new int[n];
dp[0] = 1;
dp[1] = 2;
for(int i = 2; i< n; i++){
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n - 1];
}
两个变量优化空间写法
public int climbStairs(int n) {
if(n == 0 || n == 1)
return n;
int a = 1;
int b = 2;
int result = 0;
for(int i = 2; i< n; i++){
result = a + b;
a = b;
b = result;
}
return b;
}