题目描述
斐波那契数 (通常用 F(n)
表示)形成的序列称为 斐波那契数列 。该数列由 0
和 1
开始,后面的每一项数字都是前面两项数字的和。也就是:
F(0) = 0,F(1) = 1
F(n) = F(n - 1) + F(n - 2),其中 n > 1
给定 n
,请计算 F(n)
。
示例 1:
输入:n = 2
输出:1
解释:F(2) = F(1) + F(0) = 1 + 0 = 1
示例 2:
输入:n = 3
输出:2
解释:F(3) = F(2) + F(1) = 1 + 1 = 2
示例 3:
输入:n = 4
输出:3
解释:F(4) = F(3) + F(2) = 2 + 1 = 3
提示:
解法
方法一:递推
我们定义两个变量 $a$ 和 $b$,初始时 $a = 0$, $b = 1$。
接下来,我们进行 $n$ 次循环,每次循环中,我们将 $a$ 和 $b$ 的值分别更新为 $b$ 和 $a + b$。
最后,返回 $a$ 即可。
时间复杂度 $O(n)$,其中 $n$ 为题目给定的整数 $n$。空间复杂度 $O(1)$。
| class Solution:
def fib(self, n: int) -> int:
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
|
| 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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | class Solution {
public:
int fib(int n) {
int a = 0, b = 1;
while (n--) {
int c = a + b;
a = b;
b = c;
}
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, b] = [0, 1];
while (n--) {
[a, b] = [b, a + b];
}
return a;
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | 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
}
}
|
| /**
* @param {number} n
* @return {number}
*/
var fib = function (n) {
let [a, b] = [0, 1];
while (n--) {
[a, b] = [b, a + b];
}
return a;
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | class Solution {
/**
* @param Integer $n
* @return Integer
*/
function fib($n) {
$a = 0;
$b = 1;
for ($i = 0; $i < $n; $i++) {
$temp = $a;
$a = $b;
$b = $temp + $b;
}
return $a;
}
}
|
方法二:矩阵快速幂加速递推
我们设 $\textit{Fib}(n)$ 表示一个 $1 \times 2$ 的矩阵 $\begin{bmatrix} F_n & F_{n - 1} \end{bmatrix}$,其中 $F_n$ 和 $F_{n - 1}$ 分别是第 $n$ 个和第 $n - 1$ 个斐波那契数。
我们希望根据 $\textit{Fib}(n - 1) = \begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix}$ 推出 $\textit{Fib}(n)$。也即是说,我们需要一个矩阵 $\textit{base}$,使得 $\textit{Fib}(n - 1) \times \textit{base} = \textit{Fib}(n)$,即:
$$
\begin{bmatrix}
F_{n - 1} & F_{n - 2}
\end{bmatrix} \times \textit{base} = \begin{bmatrix} F_n & F_{n - 1} \end{bmatrix}
$$
由于 $F_n = F_{n - 1} + F_{n - 2}$,所以矩阵 $\textit{base}$ 的第一列为:
$$
\begin{bmatrix}
1 \
1
\end{bmatrix}
$$
第二列为:
$$
\begin{bmatrix}
1 \
0
\end{bmatrix}
$$
因此有:
$$
\begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix} \times \begin{bmatrix}1 & 1 \ 1 & 0\end{bmatrix} = \begin{bmatrix} F_n & F_{n - 1} \end{bmatrix}
$$
我们定义初始矩阵 $res = \begin{bmatrix} 1 & 0 \end{bmatrix}$,那么 $F_n$ 等于 $res$ 乘以 $\textit{base}^{n}$ 的结果矩阵中第一行的第二个元素。使用矩阵快速幂求解即可。
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。
1
2
3
4
5
6
7
8
9
10
11
12
13 | 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]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 | 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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | 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;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | 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
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | 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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 | 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
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 | /**
* @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;
}
|