{"id":10052,"date":"2023-05-07T11:03:36","date_gmt":"2023-05-07T18:03:36","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10052"},"modified":"2023-05-07T20:59:18","modified_gmt":"2023-05-08T03:59:18","slug":"leetcode-2666-allow-one-function-call","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-2666-allow-one-function-call\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2666. Allow One Function Call"},"content":{"rendered":"\n<p>Given a function&nbsp;<code>fn<\/code>, return a new function that is identical to the original function except that it ensures&nbsp;<code>fn<\/code>&nbsp;is&nbsp;called at most once.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The first time the returned function is called, it should return the same result as&nbsp;<code>fn<\/code>.<\/li><li>Every subsequent time it is called, it should return&nbsp;<code>undefined<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> fn = (a,b,c) =&gt; (a + b + c), calls = [[1,2,3],[2,3,6]]\n<strong>Output:<\/strong> [{\"calls\":1,\"value\":6}]\n<strong>Explanation:<\/strong>\nconst onceFn = once(fn);\nonceFn(1, 2, 3); \/\/ 6\nonceFn(2, 3, 6); \/\/ undefined, fn was not called\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> fn = (a,b,c) =&gt; (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]\n<strong>Output:<\/strong> [{\"calls\":1,\"value\":140}]\n<strong>Explanation:<\/strong>\nconst onceFn = once(fn);\nonceFn(5, 7, 4); \/\/ 140\nonceFn(2, 3, 6); \/\/ undefined, fn was not called\nonceFn(4, 6, 8); \/\/ undefined, fn was not called\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= calls.length &lt;= 10<\/code><\/li><li><code>1 &lt;= calls[i].length &lt;= 100<\/code><\/li><li><code>2 &lt;= JSON.stringify(calls).length &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<p>Solution:<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">JavaScript<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"javascript\">\/\/ Author: Huahua\n\/**\n * @param {Function} fn\n * @return {Function}\n *\/\nvar once = function(fn) {\n    this.called = false;\n    return function(...args){\n      if (this.called === false) {\n        this.called = true;\n        return fn(...args);\n      } else {\n        return undefined;\n      }\n    }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a function&nbsp;fn, return a new function that is identical to the original function except that it ensures&nbsp;fn&nbsp;is&nbsp;called at most once. The first time the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10052","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10052"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10052\/revisions"}],"predecessor-version":[{"id":10055,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10052\/revisions\/10055"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}