Title Description
A frog can jump up to 1 steps at a time, or jump up to level 2. Ask the frog to jump on an n-level step with a total number of hops.
Thinking of solving problems
Recursion, from the previous step to this step the frog jumps a level or jumps two levels, the current number of steps is N, and the number of hops to the N-step is the sum of the number of n-1 and jumping n-2 steps. As you can see, this is a Fibonacci sequence .
The end condition is that when n=1, there is only one method (jumping one level); When n=0, there are 0 methods; When n=2, there are two methods (jumping one level at a time, jumping two times, jumping two levels at a time)
Code implementation
classSolution {Public:int Jumpfloor (int Number) {if( Number==0){return 0; }if( Number==1){return 1; }if( Number==2){return 2; }returnJumpfloor ( Number-1) + Jumpfloor ( Number-2); }};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Sword Point offer" jumping steps