Recursive _ frog jump step (perverted version)

Title Description

A frog can jump on a Class 1 level, you can also hop on level 2 ...... n It can also jump on stage. The frog jumped seeking a total of n grade level how many jumps.

My solution

public int JumpFloor1(int target) {
    if (target == 1) {
        return 1;
    } else {
        int total = 0;
        for (int i = 1; i < target; i++) {
            total += JumpFloorII(target - i);
        }
        return total+1;
    }
}

Gangster solution

① f(n) = f(n-1) + f(n-2) +f(n-3) + ... + f(2) + f(1)

② f(n-1) = f(n-2) +f(n-3) + ... + f(2) + f(1)

Made from ①②, f (n) = 2f (n-1);

public static int jumpFloor2(int target) {
    if (target == 1) {
        return 1;
    } else {
        return 2 * jumpFloor2(target - 1);
    }
}

Ultimate Solution

f(n) = 2f(n-1) (n>=2)
f(n) = 1 (n=1)

Recursive equations are solved:

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

2 * 2f = (( the -n 1 ) - 1) = 2 2 , f (N - 2 )

2 * 2 = * 2f (( the -n 2 ) - 1) = 2 3 , f (N - 3 )

2 * 2 = 2 * 2 * ...... * 2f (( the -n (N-3) ) -1) = 2 N-2 , f (N - (N-2) ) = 2 N-2 f (2)

2 * 2 = 2 * 2 * ...... * 2f (( the -n (N-2) ) -1) = 2 N-1 , f (N - (N-1) ) = 2 N-1 , f (1) = 2 N-1

public int JumpFloor3(int target) {
    return (int) Math.pow(2, target - 1);
}

Guess you like

Origin www.cnblogs.com/KenBaiCaiDeMiao/p/12594412.html