Prove safety Offer: jump stairs (java version)

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).

Recursion

public class Solution {
    public int JumpFloor(int target) {
    	if(target<=0)
		return 0;
        if(target==1)
            return 1;
        if(target==2)
            return 2;
        return JumpFloor(target-1)+JumpFloor(target-2);
    }
}

Dynamic Programming

= The i-th step term + i-1 i-2 of the item

public class Solution {
    public int JumpFloor(int target) {
        if(target ==1 ||target==2)
            return target;
        int n1=1;
        int n2=2;
        int count=0;
        for(int i=3;i<=target;i++){ //每个i台阶依赖i-1和i-2的方案
            count=n1+n2;
            n1=n2;
            n2=count;
        }
        return n2;   
    }
}

Guess you like

Origin blog.csdn.net/qq_43165002/article/details/93717287