{ "data": { "question": { "questionId": "2731", "questionFrontendId": "2623", "categoryTitle": "JavaScript", "boundTopicId": 2222273, "title": "Memoize", "titleSlug": "memoize", "content": "
Given a function fn
, return a memoized version of that function.
A memoized function is a function that will never be called twice with the same inputs. Instead it will return a cached value.
\n\nYou can assume there are 3 possible input functions: sum
, fib
, and factorial
.
sum
accepts two integers a
and b
and returns a + b
.fib
accepts a single integer n
and returns 1
if n <= 1
or fib(n - 1) + fib(n - 2)
otherwise.factorial
accepts a single integer n
and returns 1
if n <= 1
or factorial(n - 1) * n
otherwise.\n
Example 1:
\n\n\nInput:\nfnName = "sum"\nactions = ["call","call","getCallCount","call","getCallCount"]\nvalues = [[2,2],[2,2],[],[1,2],[]]\nOutput: [4,4,1,3,2]\nExplanation:\nconst sum = (a, b) => a + b;\nconst memoizedSum = memoize(sum);\nmemoizedSum(2, 2); // "call" - returns 4. sum() was called as (2, 2) was not seen before.\nmemoizedSum(2, 2); // "call" - returns 4. However sum() was not called because the same inputs were seen before.\n// "getCallCount" - total call count: 1\nmemoizedSum(1, 2); // "call" - returns 3. sum() was called as (1, 2) was not seen before.\n// "getCallCount" - total call count: 2\n\n\n
Example 2:
\n\n\nInput:\nfnName = "factorial"\nactions = ["call","call","call","getCallCount","call","getCallCount"]\nvalues = [[2],[3],[2],[],[3],[]]\nOutput: [2,6,2,2,6,2]\nExplanation:\nconst factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));\nconst memoFactorial = memoize(factorial);\nmemoFactorial(2); // "call" - returns 2.\nmemoFactorial(3); // "call" - returns 6.\nmemoFactorial(2); // "call" - returns 2. However factorial was not called because 2 was seen before.\n// "getCallCount" - total call count: 2\nmemoFactorial(3); // "call" - returns 6. However factorial was not called because 3 was seen before.\n// "getCallCount" - total call count: 2\n\n\n
Example 3:
\n\n\nInput:\nfnName = "fib"\nactions = ["call","getCallCount"]\nvalues = [[5],[]]\nOutput: [8,1]\nExplanation:\nfib(5) = 8 // "call"\n// "getCallCount" - total call count: 1\n\n\n
\n
Constraints:
\n\n0 <= a, b <= 105
1 <= n <= 10
0 <= actions.length <= 105
actions.length === values.length
actions[i]
is one of "call" and "getCallCount"fnName
is one of "sum", "factorial" and "fib"请你编写一个函数,它接收另一个函数作为输入,并返回该函数的 记忆化 后的结果。
\n\n记忆函数 是一个对于相同的输入永远不会被调用两次的函数。相反,它将返回一个缓存值。
\n\n你可以假设有 3 个可能的输入函数:sum
、fib
和 factorial
。
sum
接收两个整型参数 a
和 b
,并返回 a + b
。fib
接收一个整型参数 n
,如果 n <= 1
则返回 1
,否则返回 fib (n - 1) + fib (n - 2)
。factorial
接收一个整型参数 n
,如果 n <= 1
则返回 1
,否则返回 factorial(n - 1) * n
。\n\n
示例 1:
\n\n\n输入:\nfnName = \"sum\"\nactions = [\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\nvalues = [[2,2],[2,2],[],[1,2],[]]\n输出:[4,4,1,3,2]\n解释:\nconst sum = (a, b) => a + b;\nconst memoizedSum = memoize(sum);\nmemoizedSum (2, 2);// \"call\" - 返回 4。sum() 被调用,因为之前没有使用参数 (2, 2) 调用过。\nmemoizedSum (2, 2);// \"call\" - 返回 4。没有调用 sum(),因为前面有相同的输入。\n// \"getCallCount\" - 总调用数: 1\nmemoizedSum(1、2);// \"call\" - 返回 3。sum() 被调用,因为之前没有使用参数 (1, 2) 调用过。\n// \"getCallCount\" - 总调用数: 2\n\n\n
示例 2:
\n\n\n输入:\nfnName = \"factorial\"\nactions = [\"call\",\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\nvalues = [[2],[3],[2],[],[3],[]]\n输出:[2,6,2,2,6,2]\n解释:\nconst factorial = (n) => (n <= 1) ? 1 : (n * factorial(n - 1));\nconst memoFactorial = memoize(factorial);\nmemoFactorial(2); // \"call\" - 返回 2。\nmemoFactorial(3); // \"call\" - 返回 6。\nmemoFactorial(2); // \"call\" - 返回 2。 没有调用 factorial(),因为前面有相同的输入。\n// \"getCallCount\" - 总调用数:2\nmemoFactorial(3); // \"call\" - 返回 6。 没有调用 factorial(),因为前面有相同的输入。\n// \"getCallCount\" - 总调用数:2\n\n\n
示例 3:
\n\n\n输入:\nfnName = \"fib\"\nactions = [\"call\",\"getCallCount\"]\nvalues = [[5],[]]\n输出:[8,1]\n解释:\nfib(5) = 8 // \"call\"\n// \"getCallCount\" - 总调用数:1\n\n\n\n
\n\n
提示:
\n\n0 <= a, b <= 105
1 <= n <= 10
actions.length === values.length
actions[i]
为 \"call\" 和 \"getCallCount\" 中的一个fnName
为 \"sum\", \"factorial\" 和 \"fib\" 中的一个\\u7248\\u672c\\uff1a \\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\" TypeScript 5.1.6<\\/p>\\r\\n\\r\\n Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2022<\\/p>\\r\\n\\r\\n lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "\"sum\"\n[\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\n[[2,2],[2,2],[],[1,2],[]]\n\"factorial\"\n[\"call\",\"call\",\"call\",\"getCallCount\",\"call\",\"getCallCount\"]\n[[2],[3],[2],[],[3],[]]\n\"fib\"\n[\"call\",\"getCallCount\"]\n[[5],[]]",
"__typename": "QuestionNode"
}
}
}Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n
--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f \\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n