Face questions 10.2: jumping frog metamorphosis steps

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.

Programming ideas

Since the n steps, the first step there are n kinds of jump method: 1 skip, skip 2 to n stages hop
skip 1, the remaining level n-1, the remaining jumps is f (n-1)
hop stage 2, the remaining n-2 stage, the remaining jumps is F (n-2)
so that f (n) = f (n -1) + f (n-2) + ... + f (1)
since f (n-1) = f (n-2) + f (n-3) + ... + f (1)
so that f (n) = 2 * f (n-1) = 2 ^ (n- 1)

Programming

class Solution {
public:
    int jumpFloorII(int number) {
        if(number <= 2)
        {
            return number;
        }
        int a = 1;
        int fn = 1;
        for(int i = 2;i <= number;++i)
        {
            fn = 2 * a;
            a = fn;
        }
        return fn;
    }
};

Topic summary

Note Unlike the Fibonacci number.

Guess you like

Origin www.cnblogs.com/parzulpan/p/11258381.html