[Resume the question] P74 sword refers to offer: Fibonacci sequence-recursion, loop, matrix solution expansion: frog jumps the stairs problem

P74 Sword Finger Offer Interview Question 10: Fibonacci Sequence

Write a function, enter n, and find the nth term of the Fibonacci sequence.

Recursive solution:

Advantages: concise code
Disadvantages: too many repeated calculations, code efficiency is not high

// C语言代码
// 迭代解法
long long Fibonacci_iteration(unsigned n)
{
    
    
	if (n <= 0)
		return 0;
	if (n == 1)
		return 1;
	return Fibonacci_iteration(n - 1) + Fibonacci_iteration(n - 2);
}

Loop solution:

Advantages: bottom-up calculation , time complexity is O(n)

// C语言代码
// 循环解法
long long Fibonacci(unsigned n)
{
    
    
	int result[2] = {
    
     0,1 };
	if (n < 2)
		return result[n];
	long long fibNMinusOne = 1;
	long long fibNMinusTwo = 0;
	long long fibN = 0;
	for (unsigned int i = 2; i <= n; i++)
	{
    
    
		fibN = fibNMinusOne + fibNMinusTwo;
		fibNMinusTwo = fibNMinusOne;
		fibNMinusOne = fibN;
	}
	return fibN;
}

Matrix solution:

Insert picture description here

Converting the Fibonacci sequence to the power of the matrix, the time complexity is O(logn) but not practical enough

#include <cassert>

struct Matrix2By2
{
    
    
    Matrix2By2
    (
        long long m00 = 0, 
        long long m01 = 0, 
        long long m10 = 0, 
        long long m11 = 0
    )
    :m_00(m00), m_01(m01), m_10(m10), m_11(m11) 
    {
    
    
    }

    long long m_00;
    long long m_01;
    long long m_10;
    long long m_11;
};

Matrix2By2 MatrixMultiply
(
    const Matrix2By2& matrix1, 
    const Matrix2By2& matrix2
)
{
    
    
    return Matrix2By2(
        matrix1.m_00 * matrix2.m_00 + matrix1.m_01 * matrix2.m_10,
        matrix1.m_00 * matrix2.m_01 + matrix1.m_01 * matrix2.m_11,
        matrix1.m_10 * matrix2.m_00 + matrix1.m_11 * matrix2.m_10,
        matrix1.m_10 * matrix2.m_01 + matrix1.m_11 * matrix2.m_11);
}

Matrix2By2 MatrixPower(unsigned int n)
{
    
    
    assert(n > 0);

    Matrix2By2 matrix;
    if(n == 1)
    {
    
    
        matrix = Matrix2By2(1, 1, 1, 0);
    }
    else if(n % 2 == 0)
    {
    
    
        matrix = MatrixPower(n / 2);
        matrix = MatrixMultiply(matrix, matrix);
    }
    else if(n % 2 == 1)
    {
    
    
        matrix = MatrixPower((n - 1) / 2);
        matrix = MatrixMultiply(matrix, matrix);
        matrix = MatrixMultiply(matrix, Matrix2By2(1, 1, 1, 0));
    }

    return matrix;
}

long long Fibonacci_Solution3(unsigned int n)
{
    
    
    int result[2] = {
    
    0, 1};
    if(n < 2)
        return result[n];

    Matrix2By2 PowerNMinus2 = MatrixPower(n - 1);
    return PowerNMinus2.m_00;
}

Expansion: the problem of frog jumping

A frog can jump up to one or two steps at a time. Find the total number of jumping methods the frog jumps on an n-level step.

// 力扣接口
// 剑指 Offer 10- II. 青蛙跳台阶问题
int numWays(int n)
{
    
    
    if(n<2)
        return 1;
    long long  fibNMinusOne = 1;
    long long  fibNMinusTwo = 1;
    long long  fibN = 0;
    for(unsigned int i = 2; i <= n; ++ i)
    {
    
    
        fibN = (fibNMinusOne + fibNMinusTwo) % 1000000007;

        fibNMinusTwo = fibNMinusOne;
        fibNMinusOne = fibN;
    }
     return fibN;
}

Expansion 2: The problem of frog jumping

In the problem of frog jumping from steps, if the condition is changed to: a frog can jump up to one or two steps at a time... it can also jump up to n steps , at this time the frog can jump to an n step How many jumping methods are there in total?
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46613023/article/details/114320320