[Offer] to prove safety Fibonacci columns + frog jump stairs

Fibonacci number

Title: asked to enter an integer n, you output the n-th Fibonacci term of Number of

 

A1: Textbook method, recursive, but the time complexity increases with the increment n ====> impractical

A2: intermediate columns can be saved items obtained up to avoid double counting ====> time complexity of O (n)

A3: Data formula (too advanced, do not understand)

 

// O (n) method, save intermediate term

class Solution {
public:
    int Fibonacci(int n) {
        int ret[] = {0,1};
        if(n <= 1)
        {
            return ret[n];
        }
        
        long long fib_1 = 0;
        long long fib_2 = 1;
        long long fib_n = 0;
        
        for(int i = 2; i <= n ;i++)
        {
            fib_n = fib_1 + fib_2;
            fib_1 = fib_2;
            fib_2 = fib_n;
        }
        return fib_n;
    }
};

  

 

 

Related topics:

  Summing (input two integers n and m, l, 2,3 random number sequence taken from several ....... number n, and it is equal to m, which requires all of the possible combinations listed)

  Looking for the K-large (there is an array of integers, you sort of the quick thinking to find a large array of K number.)

  Generating a Gray code (encoding a set of numbers, if any two adjacent code only one bit different, this encoding is called Gray code (Gray Code), a write request function, using the recursive method of generating N-bit Gray code. given an integer n , return to an n-bit Gray code, of the order of from zero.)

 

Frog jump stairs

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

 

class Solution {
public:
    int jumpFloor(int number) {
        if(number <= 2)
        {
            return number;
        }
        long long number_1 = 1;
        long long number_2 = 2;
        long long number_n = 0;
        for (int i = 3; i <= number; i++)
        {
            number_n = number_1 + number_2;
            number_1 = number_2;
            number_2 = number_n;
        }
        return number_n;
        
    }
};

  

 

Related topics:

  A rectangular cover (2 * with a small rectangle to cover a larger rectangle, ask by small rectangles 8 2 1 * non-overlapping coverage of a large rectangle 2 * 8, the total number of ways)

  Stairs (in front of you there is a n-order staircase, you can only step on the first-order or second order. Will calculate how many different ways you can use climbed the stairs.)

  2 stairs (before you have a staircase n-th order (n> = 100 and n <500), you can only step 1 and 3rd order. Will calculate how many different ways that you can use the stairs climbed (the last layer as Pawan))

  

 

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11410038.html