We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d21b96b commit a33bbccCopy full SHA for a33bbcc
algorithms/fibonacci-number.js
@@ -6,16 +6,27 @@
6
var fib = function (n) {
7
// 0 1 1 2 3 5 8 ...
8
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
20
// 1. 递归 + 记忆化
- const memo = [0, 1];
- const fibMemo = (n) => {
- if (memo[n] != null) {
- return memo[n];
- }
- return (memo[n] = fibMemo(n - 1) + fibMemo(n - 2));
- };
-
- 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);
30
31
// 2. 递归
32
// if (n < 1) return 0;
0 commit comments