How to find the nth term of Fibonacci sequence by LeetCode
This article mainly introduces LeetCode how to find the nth item of the Fibonacci series, which is very detailed and has a certain reference value. Interested friends must read it!
A brief description of the problem
Write a function, enter n, and find the nth term of the Fibonacci series. The Fibonacci series is defined as follows:
F (0) = 0, F (1) = 1F (N) = F (N-1) + F (N-2), where N > 1. The Fibonacci series starts with 0 and 1, and the subsequent Fibonacci number is derived from the addition of the previous two numbers.
The answer needs to be modular 1e9+7 (1000000007). If the initial result is 1000000008, please return 1.
Example
Example 1:
Input: n = 2 output: 1 example 2:
Input: n = 5 output: 5
The train of thought of problem solving
Use dynamic programming to solve the problem
Problem solving procedure
Public class FibTest {public static void main (String [] args) {int n = 5; int a = fib (n); System.out.println ("a =" + a);}
Public static int fib (int n) {if (n = 0) {return 0;} if (n = 1) {return 1;} int [] dp = new int [n + 1]; dp [0] = 0; dp [1] = 1; for (int I = 2; I