To prove safety Offer_ programming problem _ Fibonacci number

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/JY_WD/article/details/102242696

Title Description

Everyone knows Fibonacci number (F (1) = 1, F (2) = 1, F (n) = F (n-1) + F (n-2) (n> = 2, n∈N *)), are now required to enter an integer n, you output the n-th Fibonacci number Fibonacci sequence of item (from 0, the first 0 is 0).
n <= 39

AC Code

public class Solution {
    public int Fibonacci(int n) {
        if(n<=1){
			return n;
		}
		else
			return Fibonacci(n-1)+Fibonacci(n-2);
		

    }
}

Topic analysis

Fibonacci of Number standard formula is: F (1) = 1, F (2) = 1, F (n) = F (n-1) + F (n-2) (n> = 3, n∈ N *)
according to a recursive formula can be written directly
in addition to optimization god refer to this

Guess you like

Origin blog.csdn.net/JY_WD/article/details/102242696
Recommended