Welcome to Subscribe On Youtube
509. Fibonacci Number
Description
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,
F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1.
Given n, calculate F(n).
Example 1:
Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
Example 2:
Input: n = 3 Output: 2 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
Example 3:
Input: n = 4 Output: 3 Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
Constraints:
0 <= n <= 30
Solutions
- Java
- C++
- Python
- Go
- TypeScript
- Javascript
- Php
- Rust
- Java 2
- C++ 2
- Python 2
- Go 2
- TypeScript 2
- Javascript 2
- Rust 2
-
class Solution { public int fib(int n) { int a = 0, b = 1; while (n-- > 0) { int c = a + b; a = b; b = c; } return a; } } -
class Solution { public: int fib(int n) { int a = 0, b = 1; while (n--) { int c = a + b; a = b; b = c; } return a; } }; -
class Solution: def fib(self, n: int) -> int: a, b = 0, 1 for _ in range(n): a, b = b, a + b return a -
func fib(n int) int { a, b := 0, 1 for i := 0; i < n; i++ { a, b = b, a+b } return a } -
function fib(n: number): number { let a = 0; let b = 1; for (let i = 0; i < n; i++) { [a, b] = [a, a + b]; } return a; } -
/** * @param {number} n * @return {number} */ var fib = function (n) { let a = 0; let b = 1; while (n--) { const c = a + b; a = b; b = c; } return a; }; -
class Solution { /** * @param Integer $n * @return Integer */ function fib($n) { if ($n == 0 || $n == 1) { return $n; } $dp = [0, 1]; for ($i = 2; $i <= $n; $i++) { $dp[$i] = $dp[$i - 2] + $dp[$i - 1]; } return $dp[$n]; } } -
impl Solution { pub fn fib(n: i32) -> i32 { let mut a = 0; let mut b = 1; for _ in 0..n { let t = b; b = a + b; a = t; } a } } -
class Solution { public int fib(int n) { int[][] a = {{1, 1}, {1, 0}}; return pow(a, n)[0][1]; } private int[][] mul(int[][] a, int[][] b) { int m = a.length, n = b[0].length; int[][] c = new int[m][n]; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < b.length; ++k) { c[i][j] = c[i][j] + a[i][k] * b[k][j]; } } } return c; } private int[][] pow(int[][] a, int n) { int[][] res = {{1, 0}}; while (n > 0) { if ((n & 1) == 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } } -
class Solution { public: int fib(int n) { vector<vector<int>> a = {{1, 1}, {1, 0}}; return qpow(a, n)[0][1]; } vector<vector<int>> mul(vector<vector<int>>& a, vector<vector<int>>& b) { int m = a.size(), n = b[0].size(); vector<vector<int>> c(m, vector<int>(n)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { for (int k = 0; k < b.size(); ++k) { c[i][j] = c[i][j] + a[i][k] * b[k][j]; } } } return c; } vector<vector<int>> qpow(vector<vector<int>>& a, int n) { vector<vector<int>> res = {{1, 0}}; while (n) { if (n & 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } }; -
import numpy as np class Solution: def fib(self, n: int) -> int: factor = np.asmatrix([(1, 1), (1, 0)], np.dtype("O")) res = np.asmatrix([(1, 0)], np.dtype("O")) while n: if n & 1: res = res * factor factor = factor * factor n >>= 1 return res[0, 1] -
func fib(n int) int { a := [][]int{{1, 1}, {1, 0}} return pow(a, n)[0][1] } func mul(a, b [][]int) [][]int { m, n := len(a), len(b[0]) c := make([][]int, m) for i := range c { c[i] = make([]int, n) } for i := 0; i < m; i++ { for j := 0; j < n; j++ { for k := 0; k < len(b); k++ { c[i][j] = c[i][j] + a[i][k]*b[k][j] } } } return c } func pow(a [][]int, n int) [][]int { res := [][]int{{1, 0}} for n > 0 { if n&1 == 1 { res = mul(res, a) } a = mul(a, a) n >>= 1 } return res } -
function fib(n: number): number { const a: number[][] = [ [1, 1], [1, 0], ]; return pow(a, n)[0][1]; } function mul(a: number[][], b: number[][]): number[][] { const [m, n] = [a.length, b[0].length]; const c = Array.from({ length: m }, () => Array.from({ length: n }, () => 0)); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { for (let k = 0; k < b.length; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } function pow(a: number[][], n: number): number[][] { let res = [[1, 0]]; while (n) { if (n & 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } -
/** * @param {number} n * @return {number} */ var fib = function (n) { const a = [ [1, 1], [1, 0], ]; return pow(a, n)[0][1]; }; function mul(a, b) { const m = a.length, n = b[0].length; const c = Array.from({ length: m }, () => Array(n).fill(0)); for (let i = 0; i < m; ++i) { for (let j = 0; j < n; ++j) { for (let k = 0; k < b.length; ++k) { c[i][j] += a[i][k] * b[k][j]; } } } return c; } function pow(a, n) { let res = [ [1, 0], [0, 1], ]; while (n > 0) { if (n & 1) { res = mul(res, a); } a = mul(a, a); n >>= 1; } return res; } -
impl Solution { pub fn fib(n: i32) -> i32 { let a = vec![vec![1, 1], vec![1, 0]]; pow(a, n as usize)[0][1] } } fn mul(a: Vec<Vec<i32>>, b: Vec<Vec<i32>>) -> Vec<Vec<i32>> { let m = a.len(); let n = b[0].len(); let mut c = vec![vec![0; n]; m]; for i in 0..m { for j in 0..n { for k in 0..b.len() { c[i][j] += a[i][k] * b[k][j]; } } } c } fn pow(mut a: Vec<Vec<i32>>, mut n: usize) -> Vec<Vec<i32>> { let mut res = vec![vec![1, 0], vec![0, 1]]; while n > 0 { if n & 1 == 1 { res = mul(res, a.clone()); } a = mul(a.clone(), a); n >>= 1; } res }