EasyDynamic ProgrammingRecursionMemoizationMath

Fibonacci Number

LeetCode
2 approaches, code in all languages

The Fibonacci numbers start with F(0) = 0 and F(1) = 1, and after that every number is the sum of the two immediately before it: F(n) = F(n - 1) + F(n - 2) for n greater than 1.

Given an index n, return the value F(n).

Example 1

Input: n = 2

Output: 1

F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2

Input: n = 5

Output: 5

The sequence up to index 5 is 0, 1, 1, 2, 3, 5, so F(5) = 5.

Constraints

  • 0 ≤ n ≤ 30
  • The result fits inside a signed 32-bit integer.
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now