Skip to main content

斐波那契数

题目

求斐波那契数.

示例

输入: 10

输出: 5

题解

关于斐波那契数的各种花式玩法, 可参考动态规划.

/**
* @param {number} N
* @return {number}
*/
var fib = function (N) {
if (N <= 1) return N

let prev = 0
let curr = 1

for (let i = 2; i <= N; i++) {
const sum = prev + curr
prev = curr
curr = sum
}
return curr