"Sword Pointing Offer" - Java Implementation - Abnormal Jumping Steps

Topic description

A frog can jump up 1 steps at a time, it can jump up 2 steps...it can also jump up n steps. Find how many ways the frog can jump up a n-level stair.

ideas

Because there are n steps, there are n jumps in the first step: jump 1, jump 2, jump 1 to n, and if n-1 is left, the remaining jump is f(n-1).
So f(n)=f(n-1)+f(n-2)+…+f(1)
because f(n-1)=f(n-2)+f(n-3)+…+ f(1)
so f(n)=2*f(n-1)

code

public class Solution {
    public int JumpFloorII(int target) {
        if(target == 1)
            return 1;
        else if(target ==2)
            return 2;
        else 
        {
            return JumpFloorII(target-1)*2;
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325640416&siteId=291194637