Sword refers to offer (3) Fibonacci sequence + step jump + abnormal step jump

Record and review some question types of Jianzhi offer~


Fibonacci sequence

Title description
Everyone knows the Fibonacci sequence. Now it is required to input an integer n. Please output the nth term of the Fibonacci sequence (starting from 0, the 0th term is 0, and the 1st term is 1).
n<=39


The Fibonacci sequence refers to a sequence of 1,1,2,3,5,8,13,21,34..., this sequence is equal to the sum of the first two numbers starting from the third number.
The question requires to start from 0, the 0th item is 0, and the 1st item is 1, then we can calculate by recursion.

public class Solution {
    
    
    public int Fibonacci(int n) {
    
    
        //当n=0的时候,返回0
        if(n == 0) return 0;
        //当n=1的时候,返回1
        if(n == 1) return 1;
        //递归返回n-1 和 n-2 得出的数值
        return Fibonacci(n-1) + Fibonacci(n-2);
    }
}

Step up

Topic description
A frog can jump up to 1 step or 2 steps at a time. Find the total number of jumping methods the frog jumps on an n-level step (different order counts as different results).


There are n steps, and the frog can only jump 1 or 2 steps at a time. After jumping 1 step for the first time, the remaining steps are n-1. Similarly, if you jump out of 2 steps for the first time, the remaining steps are n-2. Then n-1 level can also choose to start skipping level 1 or level 2. Then it's the same as recursion, and the result will be exactly the same as the Fibonacci sequence of the previous question . This is done in a non-recursive way~


public class Solution {
    
    
    public int JumpFloor(int target) {
    
    
        //当只有1-2阶的时候,直接输出答案 
        if (target == 1) return 1;
        if (target == 2) return 2;
        //记录1级阶梯的高度
        int f1 = 1;
        //记录2级阶梯的高度
        int f2 = 2;
        //设置一个目标值
        int f3 = 0;
        //从第三阶梯开始循环到目标阶梯
        for (int i = 3; i <= target; i++) {
    
    
            //跳第三阶梯的跳法次数 = 第一阶梯 + 第二阶梯的次数 以此类推
            f3 = f1 + f2;
            //将第二阶梯的跳法次数 赋给第一阶梯
            f1 = f2;
            //将第三阶梯的跳法次数 赋给第二阶梯 
            f2 = f3;
        }
        return f3;
    }
}

Abnormal jump

Title description
A frog can jump up to 1 level or 2 levels at a time...it can also jump to n levels. Find the total number of jumping methods the frog jumps on an n-level step.


Problem analysis : For
example, if there is a three- step step, a frog can jump one step at a time , or one step and then two steps , or two steps and then one step , or just three steps .
Therefore, a 3-level step can be divided into the following four jumping methods, 111, 12, 21, and 3. So we can continue to follow the recursive idea of ​​the previous question. The 3 steps can be skipped by 1 step, or 2 steps can be skipped first, or even 3 steps can be skipped directly, so: f(3) = f(3- 1) + f(3-2) + f(3-3) .
f(3-3)=f(0)=1 The 3-step ladder jumps 3 steps at a time, so there is only one jumping method. f(3-2)=f(1)=1 first jump 2 steps, and there is only 1 step left, so there is only one jumping method. f(3-1)=f(2)=2 Jump 1 level first, and 2 levels are left. There are two ways to jump.
So 3 steps can be obtained: f(3) = f(0) + f(1) + f(2)
so if there are n levels: f(n) = f(0)+f(1)+f(2 )+…+f(n-2)+f(n-1)
However, n-1 level can also be obtained: f(n-1) = f(0)+f(1)+f(2)+…+ f(n-2),
so n steps can get f(n) = 2 * f(n-1)
and thenSolve the case ~ You can directly calculate the result through recursion.

public class Solution {
    
    
    public int JumpFloorII(int target) {
    
    
        //总台阶是1的时候,只有1种跳法
        if(target == 1) return 1;
        //总台阶是2的时候,有1,2 2,1两种跳法
        if(target == 2) return 2;
        //返回2* f(n-1)的结果 ,递归得到结果
        return 2*JumpFloorII(target - 1);
    }
}

Guess you like

Origin blog.csdn.net/qq_41762594/article/details/106102544