Skip to content

Commit 9a403ec

Browse files
author
v_yongztan
committed
剑指 Offer 10- I. 斐波那契数列.
1 parent 0e83245 commit 9a403ec

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lib/leetcode_1_fib.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// 剑指 Offer 10- I. 斐波那契数列.
2+
// https://leetcode-cn.com/problems/fei-bo-na-qi-shu-lie-lcof/
3+
// start 21:45 end
4+
5+
/**
6+
* @param {number} n
7+
* @return {number}
8+
*/
9+
var fib = function (n) {
10+
let res = [0, 1]
11+
12+
function f(n) {
13+
if (typeof res[n] !== 'undefined') {
14+
return res[n]
15+
}
16+
// console.log(`res[${n}] = f(${n - 1}) + f(${n - 2})`)
17+
res[n] = (f(n - 1) + f(n - 2)) % 1000000007
18+
// console.log(res)
19+
return res[n]
20+
}
21+
return f(n)
22+
};
23+
24+
console.log(fib(81))

0 commit comments

Comments
 (0)