Fibonacci Number
Easy · Math, Dynamic Programming, Recursion, Memoization
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
Given n, calculate F(n).
Example 1
Input: n = 2 Output: 1
F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2
Input: n = 3 Output: 2
F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3
Input: n = 4 Output: 3
F(4) = F(3) + F(2) = 2 + 1 = 3.
Constraints
- 0 <= n <= 30
Starter code (python)
class Solution(object):
def fib(self, n):
"""
:type n: int
:rtype: int
"""
Related problems
- Add Two Numbers (Medium)
- Longest Palindromic Substring (Medium)
- Reverse Integer (Medium)
Previous: Numbers With Same Consecutive Differences · Next: Subarray Sums Divisible by K · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.