comments | difficulty | edit_url | tags | ||||
---|---|---|---|---|---|---|---|
true |
简单 |
|
斐波那契数 (通常用 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
提示:
0 <= n <= 30
我们定义两个变量
接下来,我们进行
最后,返回
时间复杂度
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;
}
}
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;
}
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;
};
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) = \begin{bmatrix} F_{n - 1} & F_{n - 2} \end{bmatrix}$ 推出
由于
第二列为:
因此有:
我们定义初始矩阵 $res = \begin{bmatrix} 1 & 0 \end{bmatrix}$,那么
时间复杂度
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]
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;
}
};
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;
}
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
}
/**
* @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;
}