# Whether it&#39;s a frog jumping the stairs or who climbs the stairs, you can go up

The question of climbing stairs is also a very classic interview question. You can change various characters and animals, such as frogs and rabbits jumping steps, Zhang Sanlisi climbing stairs and so on.

The title will be similar to the following:

Suppose you are climbing stairs and it takes n steps to get you to the top, you can climb 1 or 2 steps each time. How many different ways can you get to the top of a building?

Assuming there are 2 steps, there are two ways to climb to the top:

1. 1 stair + 1 stair
2. 2 steps

Assuming there are 3 steps, there are three ways to get to the top:

1. 1 stair + 1 stair + 1 stair
2. 1 step + 2 steps
3. 2 steps + 1 step

Assuming there are 5 steps, there are eight ways to get to the top:

1. 1 slab + 1 slab + 1 slab + 1 slab + 1 slab
2. 1 stair + 1 stair + 1 stair + 2 stair
3. 1 stair + 2 stair + 2 stair
4. 2 stairs + 2 stairs + 1 stairs
5. 2 stairs + 1 stairs + 2 stairs
6. 2 stairs + 1 stairs + 1 stairs + 1 stairs
7. 1 stairs + 2 stairs + 1 stairs + 1 stairs
8. 1 stairs + 1 stairs + 2 stairs + 1 stairs

With three examples, let's push it backwards into code.

Suppose the method to climb to the top of the building is `f(n)` , there are two possibilities according to the meaning of the title

• Climb one step: `f(n-1)`
• Climb two steps: `f(n−2)`

There are only these two possibilities, adding these two possibilities together, you can get the recurrence formula:

`f(n) = f(n-1) + f(n-2)`

This way we can use recursion to achieve this:

But we also need to deal with boundary issues during recursion, such as `f(1) = 1` , `f(2) = 2` , in these two cases we can return directly without recursion.

So you can get a recursive function like this:

``` ```function climbStairs(\$n) {
if (\$n == 1) return 1;
if (\$n == 2) return 2;
return climbStairs(\$n - 1) + climbStairs(\$n - 2);
}

echo climbStairs(2); // 2
echo climbStairs(3); // 3
echo climbStairs(5); // 8``````

Let's see if using a loop solves the problem?

Suppose we use a loop to do this, then we can use an array to hold the values that have been calculated, let's say an array `dp[]` to save how many ways to climb a step:

`dp[n] = dp[n-1] + dp[n-2]`

Like recursion, it also satisfies `dp[1] = 1` , `dp[2] = 2` , you can get the following function

``` ```function climbStairs(\$n) {
\$dp = [];
\$dp[1] = 1;
\$dp[2] = 2;
for (\$i = 3; \$i <= \$n; \$i++) {
\$dp[\$i] = \$dp[\$i - 1] + \$dp[\$i - 2];
}
return \$dp[\$n];
}``````

When using recursion, we start from the last step and go to the bottom step to find it. It is not sure whether the next one is the end point, we can only find it one by one.

When using the loop, we start from the first step and climb up. At this time, we know the number of the previous methods, just use it directly, without having to recalculate.

``` ```echo climbStairs(3), PHP_EOL; // 3
echo climbStairs(4), PHP_EOL; // 5
echo climbStairs(5), PHP_EOL; // 8
echo climbStairs(6), PHP_EOL; // 13
echo climbStairs(7), PHP_EOL; // 21
echo climbStairs(8), PHP_EOL; // 34``````

The above enumeration can also verify our conjecture, in fact we only need to know: `当前方法个数 = 前一个方法个数 + 前前一个方法个数` .

Suppose `f` is the number of current methods, `a` and `b` respectively `前一个` and `前前一个` number of methods , you can get a new way of looping:

We started climbing from the `0` 184ea7be5ff89af2bdb79045ee888de4---stair, from the `0` to the `0` stair, we can also use it as a method, that is `f=1`

``` ```function climbStairs(\$n) {
\$a = \$b = 0;
\$f = 1;

for (\$i = 1; \$i <= \$n; \$i++) {
\$a = \$b;
\$b = \$f;
\$f = \$a + \$b;
}
return \$f;
}``````

The above is a dynamic programming code. If you look at it directly, it may be difficult to understand why it is written in this way. After verification and understanding of recursion and looping, it looks very simple.

Don't panic when encountering algorithm questions in the interview. You can start with a simple and violent method, and then try to see if there is an optimal solution.

At the same time, it is also suggested that when you have free time, you can debunk and brush algorithm questions and understand some related concepts and ideas, so that you will not panic when encountering algorithm questions and do not know where to start.

In addition, you should be familiar with the language used, especially some built-in functions. For example, when verifying whether a number is a palindrome, PHP can use the strrev function, invert it and then compare it to directly judge Yes or not.

``` ```\$n1 = 21;
\$n2 = 22;
\$n3 = -22;
var_dump(strrev(\$n1) == \$n1);
var_dump(strrev(\$n2) == \$n2);
var_dump(strrev(\$n3) == \$n3);``````

Remember to review in time after the interview, the next time will definitely be better than this time~

This article participated in the SegmentFault Sifu essay "How to "anti-kill" the interviewer?" , you are welcome to join.

##### 沈唁
Swoole & Hyperf & Docsify 开发组成员
1.7k 声望
1.2k 粉丝
0 条评论