class Solution {
public int climbStairs(int n) {
if (n == 0 || n == 1) return 1;
int[] res = new int[n+1];
res[0] = res[1] = 1;
for (int i = 2; i <= n; i++) {
res[i] = res[i-1] + res[i-2];
}
return res[n];
}
}
注:
可能是动态规划最基础的题了。改进的话不用数组存储,可以只存两个数。