Experience:
-
Like a half an hour of permutations and combinations, high school mathematics learning is not good, I feel too many cases to be considered, of course, the final result can not be run over;
-
Looked helpless discuss solution to a problem, a look on the ignorant force, the direct use of Fibonacci number, a lot of people did not write thinking;
-
Like a long time together and students discuss why the column was found with that deed Fibonacci number f (n) = f (n - 1) + f (n - 2)
-
Example 4 Take sub-step layer. First, consider how a first leg, a step or two-step
- Step: The remaining three steps. So how can the rest of the three steps to go, but also by the three-step how to come out before coming performed;
- Two-step: The remaining two steps. So how can the rest of the two-step walk, also by two-step how to come out before coming performed;
- f(4) = f(3) + f(2)。
-
Well, the rest also and so on.
-
The first condition is to launch its own 1 and 2, excluding the special case of layer 0
topic
Suppose you are climbing stairs. N order you need to get to the roof.
Every time you can climb one or two steps. How many different ways can climb to the roof of it?
Note: Given n is a positive integer.
Example 1:
Input: 2
Output: 2
explanation: There are two methods can climb the roof.
- 1 + 1-order-order
- 2 order
Example 2:
Input: 3
Output: 3
Explanation: There are three methods can climb to the roof.
- Step 1 + 1 + 1-order-order
- 2 + 1-order-order
- 2 + 1-order-order
Code:
int climbStairs(int n) {
vector<int> result(n);
if (n == 0 || n == 1 || n == 2) return n;
result[0] = 1;
result[1] = 2;
for (int i = 2; i < n; i++) {
result[i] = result[i - 1] + result[i - 2];
}
return result[n - 1];
}