To prove safety Offer_ programming problem _ a step jump, jump metamorphosis steps

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/JY_WD/article/details/102505623

Step jump

Title Description

A frog can jump on a Class 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).

AC Code

public class Solution {
    public int JumpFloor(int target) {
        if(target==1)
            return 1;
        else if(target==2)
            return 2;
        else if(target==3)
            return 3;
        else 
            return JumpFloor(target-1)+JumpFloor(target-2);

    }
}

Topic analysis

Thinking: Jump n of steps corresponding to n-1 and n-2 and the stairs of
reasons: n of steps is equivalent to a jump stage n-1 a n-2 order and a stage jump order 2



Abnormal jump stairs

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.

AC Code

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

Topic analysis

F (n-) = F (n--. 1) + F (n--2) + ... + F (. 1)
F (n--. 1) = F (n--2) + ... F (. 1)
to give: f (n) = 2 * f (n-1 )
derived equation derived n-1 th power of 2

Guess you like

Origin blog.csdn.net/JY_WD/article/details/102505623