Recursion (iteration) -- abnormal step jumping

Title Description
A frog can jump up one step or two at a time It can also jump to level n. Find out how many ways the frog can jump to an n-level step.

Topic analysis
According to the previous topic: frog only jumps 1 or 2, it can be concluded that it is a Fibonacci problem, that is, a[n]=a[n-1]+a[n-2], then a[n]=a[n-1]+a[n-2]+a[n-3], when it can jump 1, 2, 3 steps

As for this problem, the premise is that n steps will have a jump of n steps. The analysis is as follows:

f(1) = 1
f(2) = f(2-1) + f(2-2)         //f(2-2) Express2Step skip2Degree.
f(3) = f(3-1) + f(3-2) + f(3-3) 
f(n) = f(n-1) + f(n-2) + f(n-3) + ... + f(n-(n-1)) + f(n-n) 

//Explain: 
1)There f(n) The representative is n One step at a time1,2,...n Jump number of order.
2)n = 1Only when1Seed jumping method f(1) = 1
3) n = 2There are two ways to jump, once1Order or2It's back to the problem(1) ,f(2) = f(2-1) + f(2-2) 
4) n = 3There are three ways to jump,1Order,2Order,3Order,
    //So it's the first time1After the steps are left: f(3-1);First jump out2Order, left f(3-2);For the first time3Step, then the rest f(3-3)
    //So the conclusion is f(3) = f(3-1)+f(3-2)+f(3-3)
5) n = n There will be n The middle jump,1Order,2rank...n The conclusion is as follows:
    f(n) = f(n-1)+f(n-2)+...+f(n-(n-1)) + f(n-n) => f(0) + f(1) + f(2) + f(3) + ... + f(n-1) 
6) It is already a conclusion, but for simplicity, we can continue to simplify:
    f(n-1) = f(0) + f(1)+f(2)+f(3) + ... + f((n-1)-1) = f(0) + f(1) + f(2) + f(3) + ... + f(n-2)
    f(n) = f(0) + f(1) + f(2) + f(3) + ... + f(n-2) + f(n-1) = f(n-1) + f(n-1)
    //It can be concluded that:
    f(n) = 2*f(n-1)
7) Come to a final conclusion,stay n Steps, once1,2,...n In the jump mode of step, the total jump method is:
        | 1       ,(n=0 ) 
f(n) =  | 1       ,(n=1 )
        | 2*f(n-1),(n>=2)

Method 1: recursion

function JumpFloorII(target) {
        if (target <= 0) {
            return -1;
        } else if (target == 1) {
            return 1;
        } else {
            return 2 * JumpFloorII(target - 1);
        }
    }
    console.log(JumpFloorII(2));

Method 2: iteration

function jumpFloorII(number) {
        var f=1,fn=1;
        for(var i=2;i<=number;i++){
            fn=2*f;
            f=fn;
        }
        return fn;
    }

Keywords: REST

Added by mikeeeeeeey on Thu, 23 Jan 2020 18:22:55 +0200