Suppose you are climbing stairs. You need n
steps to get to the top of the building.
Every time you can climb 1
or 2
a step. How many different ways can you get to the top of the building?
Idea 1: Recursion
int climbStairs(int n){
int num[n+1];
num[0]=1,num[1]=1;
for(int i=2;i<=n;i++){
num[i]=num[i-1]+num[i-2];
}
return num[n];
}
analyze:
The method of the next step of this question is equal to the number of methods of the previous step plus the first two steps, and recurses continuously to n to get the answer
Summarize:
This question examines the application of recursion, and it can be solved by converting the step problem into a recursive problem