We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 180a21e commit 145625bCopy full SHA for 145625b
day11/leetcode2623.js
@@ -0,0 +1,32 @@
1
+// 2623. Memoize
2
+// URL -> https://leetcode.com/problems/memoize/
3
+
4
+/**
5
+ * @param {Function} fn
6
+ * @return {Function}
7
+ */
8
+function memoize(fn) {
9
+ let cache = {}
10
+ return function (...args) {
11
+ const key = args.join(",")
12
+ if ((key in cache)) {
13
+ return cache[key];
14
+ }
15
+ const calc = fn(...args);
16
+ cache[key] = calc;
17
+ return calc;
18
19
20
+}
21
22
23
24
+* let callCount = 0;
25
+* const memoizedFn = memoize(function (a, b) {
26
+* callCount += 1;
27
+* return a + b;
28
+* })
29
+* memoizedFn(2, 3) // 5
30
31
+* console.log(callCount) // 1
32
+*/
0 commit comments