Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the nth term of Fibonacci sequence by LeetCode

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report