【剑指offer】10.3 变态跳台阶 不用麻烦了,青蛙很忙的,青蛙不会飞,从零开始跳

在这里插入图片描述
还是先找出关系式 不用麻烦了,青蛙很忙的,青蛙不会飞,从零开始跳

台阶 跳法
1 1(1)
2 2(1 1, 2)
3 4(1 1 1,1 2,2 1,3)
4 8 (1 1 1 1,1 1 2,1 2 1,1 3,2 1 1,2 2,3 1,4)

可以看出来 关系式是 N = N-1 +2*(N-2)

JAVA:

public class Solution {
    public int JumpFloorII(int target) {
        int[] save = {0,1,2};
        if(target >= 0 && target<3)
            return save[target];
        int all = 0;
        int first = 2;
        int end =1;
        for(int i =3;i<=target;i++){
            all = first + 2*end;
            end = first;
            first = all;
        }
        return all;
    }
}

猜你喜欢

转载自blog.csdn.net/beauman/article/details/89535124