1. 数组解构
var fibonacci = function (n) {
let n1 = 1;
let n2 = 1;
for (let i = 2; i < n; i++) {
[n1, n2] = [n2, n1 + n2]
}
return n2
}
2. 利用矩阵乘积来求解(时间复杂度lgn)
let MatrixMultiply = (m, n) =>{
let res = [];
for (let i = 0; i < m.length; i++) {
res[i] = [];
let mRow = m[i];
for(let j = 0; j < mRow.length; j++) {
let num = 0;
for(let k = 0; k < mRow.length; k++) {
num += mRow[k] * n[k][j];
}
res[i][j] = num;
}
}
return res;
}
let Matrix = (n) => {
let base = [[1, 1], [1, 0]];
let ans = [[1, 0], [0, 1]];
while (n) {
if(n & 1) {
ans = MatrixMultiply(ans, base);
}
base = MatrixMultiply(base, base);
n >>>= 1;
}
return ans[0][1];
}