Skip to content

Commit a33bbcc

Browse files
committed
509. 斐波那契数
1 parent d21b96b commit a33bbcc

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

algorithms/fibonacci-number.js

+20-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,27 @@
66
var fib = function (n) {
77
// 0 1 1 2 3 5 8 ...
88

9+
// 0. DP
10+
if (n <= 1) return n;
11+
const dp = [0, 1];
12+
for (let i = 2; i <= n; i++) {
13+
const sum = dp[0] + dp[1];
14+
dp[0] = dp[1];
15+
dp[1] = sum;
16+
}
17+
return dp[1];
18+
19+
920
// 1. 递归 + 记忆化
10-
const memo = [0, 1];
11-
const fibMemo = (n) => {
12-
if (memo[n] != null) {
13-
return memo[n];
14-
}
15-
return (memo[n] = fibMemo(n - 1) + fibMemo(n - 2));
16-
};
17-
18-
return fibMemo(n);
21+
// const memo = [0, 1];
22+
// const fibMemo = (n) => {
23+
// if (memo[n] != null) {
24+
// return memo[n];
25+
// }
26+
// return (memo[n] = fibMemo(n - 1) + fibMemo(n - 2));
27+
// };
28+
29+
// return fibMemo(n);
1930

2031
// 2. 递归
2132
// if (n < 1) return 0;

0 commit comments

Comments
 (0)