标签:技术 code class ret asc 记忆化 递归 ons 结果
function fibonacciMemoization(n) {
const memo = [0, 1, 1]
if (n < 1) return 0
if (n <= 2) return memo[n]
const fibonacci = n => {
if (memo[n]) {
return memo[n]
} else {
if (!memo[n - 1]) { memo[n - 1] = fibonacci(n - 1) }
if (!memo[n - 2]) { memo[n - 2] = fibonacci(n - 2) }
memo[n] = memo[n - 1] + memo[n - 2]
return memo[n]
}
}
return fibonacci(n)
}
function fibonacci(n) {
if (n <= 0) return 0
if (n < 2) return n
return fibonacci(n - 1) + fibonacci(n - 2)
}
标签:技术 code class ret asc 记忆化 递归 ons 结果
原文地址:https://www.cnblogs.com/guojbing/p/13376570.html