题解 | #斐波那契数列#
斐波那契数列
http://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3
function Fibonacci(n) { if (n < 0 || Math.floor(n) !== n) { return -1; } if (n === 1 || n === 2) { return 1; } const cache = [1, 1]; for (let i = 3; i < n; i++) { const temp = cache[1] + cache[0]; cache[0] = cache[1]; cache[1] = temp; } return cache[0] + cache[1]; } module.exports = { Fibonacci : Fibonacci };