Ideas:
The question asked how many different ways to climb to the top of the building , it is natural to think of using the Fibonacci sequence
Relying on the result of the previous time every time, think of using dp
Code:
class Solution {
public int climbStairs(int n) {
int[] dp=new int[n+1];
dp[0]=1;
dp[1]=1;
for(int i=2;i<=n;i++){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n];
}
}
break down:
1) To prevent subscript overflow, when dp is declared, the length is added 1 (n+1) on the basis of n
2) This is the first form of dp: ( linear )
Rely only on a limited number (two) of previous states