{"id":52175,"date":"2024-12-03T17:19:53","date_gmt":"2024-12-03T09:19:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52175\/"},"modified":"2024-12-03T17:19:53","modified_gmt":"2024-12-03T09:19:53","slug":"leetcode-%e5%85%81%e8%ae%b8%e4%b8%80%e4%b8%aa%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52175\/","title":{"rendered":"Leetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Leetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aLeetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1731998431673c32dfd3924.jpg\" class=\"aligncenter\" title=\"Leetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u63d2\u56fe\" alt=\"Leetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u63d2\u56fe\" \/><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570 fn\uff0c\u8fd4\u56de\u4e00\u4e2a\u4e0e\u539f\u59cb\u51fd\u6570\u76f8\u540c\u7684\u65b0\u51fd\u6570\uff0c\u9664\u4e86\u5b83\u786e\u4fdd fn \u6700\u591a\u88ab\u8c03\u7528\u4e00\u6b21\u3002<\/p>\n<p>\u7b2c\u4e00\u6b21\u8c03\u7528\u8fd4\u56de\u7684\u51fd\u6570\u65f6\uff0c\u5b83\u5e94\u8be5\u8fd4\u56de\u4e0e fn \u76f8\u540c\u7684\u7ed3\u679c\u3002<br \/> \u968f\u540e\u6bcf\u6b21\u8c03\u7528\u5b83\u65f6\uff0c\u5b83\u90fd\u5e94\u8be5\u8fd4\u56de\u672a\u5b9a\u4e49\u3002<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<p><strong>\u8f93\u5165\uff1a<\/strong><\/p>\n<p> fn = (a,b,c) =&gt; (a + b + c), \u8c03\u7528 = [[1,2,3],[2,3,6]]<\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>\n\n**explanation:**\n\n\n<\/pre>\n<p>const oncefn = once(fn);<br \/> \u4e00\u6b21fn(1,2,3); \/\/ 6<br \/> \u4e00\u6b21fn(2,3,6); \/\/ \u672a\u5b9a\u4e49\uff0cfn \u672a\u88ab\u8c03\u7528<\/p>\n<pre>\n\n**example 2:**\n\n**input:**\n\n ```fn = (a,b,c) =&gt; (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]```\n\n\n**output:**\n\n ```[{\"calls\":1,\"value\":140}]```\n\n\n**explanation:**\n\n\n<\/pre>\n<p>const oncefn = once(fn);<br \/> \u4e00\u6b21fn(5,7,4); \/\/ 140<br \/> \u4e00\u6b21fn(2,3,6); \/\/ \u672a\u5b9a\u4e49\uff0cfn \u672a\u88ab\u8c03\u7528<br \/> \u4e00\u6b21fn(4,6,8); \/\/ \u672a\u5b9a\u4e49\uff0cfn \u672a\u88ab\u8c03\u7528<\/p>\n<pre>\n\n\n**constraints:**\n\n`calls` is a valid json array\n\n\n\n<\/pre>\n<p>1 &lt;= \u901a\u8bdd\u957f\u5ea6 &lt;= 10<br \/> 1 &lt;= \u8c03\u7528[i].length &lt;= 100<br \/> 2 &lt;= json.stringify(calls).length &lt;= 1000<\/p>\n<pre>\n\n\n*Solution*\n\nIn this case, we are required to create a higher-order function(a function that returns another function) [Read more about high-order functions here](https:\/\/www.freecodecamp.org\/news\/higher-order-functions-explained\/#:~:text=JavaScript%20offers%20a%20powerful%20feature,even%20return%20functions%20as%20results.)\n\nWe should make sure that the original function `fn` is only called once regardless of how many times the second function is called.\n\nIf the function fn has been not called, we should call the function `fn` with the provided arguments `args`. Else, we should return `undefined`\n\n\n_Code solution_\n\n\n\n``` sh\n\/**\n * @param {Function} fn\n * @return {Function}\n *\/\nvar once = function (fn) {\n    \/\/ if function === called return undefined, \n    \/\/ else call fn with provide arguments\n    let executed = false;\n    let result;\n\n    return function (...args) {\n        if (!executed) {\n            executed = true;\n            result = fn(...args);\n            return result;\n        } else {\n            return undefined;\n        }\n    }\n};\n\n\/**\n * let fn = (a,b,c) =&gt; (a + b + c)\n * let onceFn = once(fn)\n *\n * onceFn(1,2,3); \/\/ 6\n * onceFn(2,3,6); \/\/ returns undefined without calling fn\n *\/\n\n<\/pre>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aLeetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u66ff\u6362\u5b57\u7b26\u4e32\u524d\u7f00\u548c\u540e\u7f00\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528 \u6536\u85cf \u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60\u6587\u7ae0\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300aLeetcode #\u5141\u8bb8\u4e00\u4e2a\u51fd\u6570\u8c03\u7528\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01 \u7ed9\u5b9a\u4e00\u4e2a\u51fd\u6570 fn\uff0c\u8fd4\u56de\u4e00\u4e2a\u4e0e\u539f\u59cb\u51fd\u6570\u76f8\u540c\u7684\u65b0\u51fd\u6570\uff0c\u9664\u4e86\u5b83\u786e\u4fdd fn \u6700\u591a\u88ab\u8c03\u7528\u4e00\u6b21\u3002 \u7b2c\u4e00\u6b21\u8c03\u7528\u8fd4\u56de\u7684\u51fd\u6570\u65f6\uff0c\u5b83\u5e94\u8be5\u8fd4\u56de\u4e0e fn \u76f8\u540c\u7684\u7ed3\u679c\u3002 \u968f\u540e\u6bcf\u6b21\u8c03\u7528\u5b83\u65f6\uff0c\u5b83\u90fd\u5e94\u8be5\u8fd4\u56de\u672a\u5b9a\u4e49\u3002 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1a fn = (a,b,c) =&gt; (a + b + c), \u8c03\u7528 = [[1,2,3],[2,3,6]] \u8f93\u51fa\uff1a **explanation:** const oncefn = once(fn); \u4e00\u6b21fn(1,2,3); \/\/ 6 \u4e00\u6b21fn(2,3,6); \/\/ \u672a\u5b9a\u4e49\uff0cfn \u672a\u88ab\u8c03\u7528 **example 2:** **input:** &#8220;`fn = (a,b,c) =&gt; (a * b * c), calls = [[5,7,4],[2,3,6],[4,6,8]]&#8220;` [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52175","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52175","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52175"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52175\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}