斐波那契数
题目
斐波那契数(通常用 F(n)
表示)形成的序列称为斐波那契数列. 该数列由 0
和 1
开始, 后面的每一项数字都是前面两项数字的和. 也就是:
F(0) = 0, F(1) = 1
F(n) = F(n - 1) + F(n - 2), 其中 n > 1
给定 n
, 请计算 F(n)
.
提示:
0 <= n <= 30
示例
输入: n = 2
输出: 1
解释: F(2) = F(1) + F(0) = 1 + 0 = 1
输入: n = 3
输出: 2
解释: F(3) = F(2) + F(1) = 1 + 1 = 2
输入: n = 4
输出: 3
解释: F(4) = F(3) + F(2) = 2 + 1 = 3
题解
- JavaScript - 动态规划一维数组
- JavaScript - 动态规划优化版
/**
* @param {number} n
* @return {number}
*/
var fib = function (n) {
if (n < 2) return n
const dp = new Array(n + 1).fill(0)
dp[0] = 0
dp[1] = 1
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2]
}
return dp[n]
}
/**
* @param {number} n
* @return {number}
*/
var fib = function (n) {
if (n < 2) 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
}