{"id":53671,"date":"2024-12-03T14:53:20","date_gmt":"2024-12-03T06:53:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53671\/"},"modified":"2024-12-03T14:53:20","modified_gmt":"2024-12-03T06:53:20","slug":"eetcode-%e5%af%b9%e6%95%b0%e7%bb%84%e4%b8%ad%e7%9a%84%e6%af%8f%e4%b8%aa%e5%85%83%e7%b4%a0%e5%ba%94%e7%94%a8%e5%8f%98%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53671\/","title":{"rendered":"#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241203\/1733209231674eac8f1940e.jpg\" class=\"aligncenter\" title=\"#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362\u63d2\u56fe\" alt=\"#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362\u63d2\u56fe\" \/><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 arr \u548c\u4e00\u4e2a\u6620\u5c04\u51fd\u6570 fn\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u8f6c\u6362\u3002<\/p>\n<p>\u8fd4\u56de\u7684\u6570\u7ec4\u5e94\u521b\u5efa\u4e3a returnedarray[i] = fn(arr[i], i).<\/p>\n<p>\u8bf7\u5728\u4e0d\u4f7f\u7528\u5185\u7f6earray.map\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\u89e3\u51b3\u3002<\/p>\n<p><strong>\u793a\u4f8b1\uff1a<\/strong><\/p>\n<p><strong>\u8f93\u5165\uff1a<\/strong> arr = [1,2,3], fn = function plusone(n) { return n + 1; }<br \/><strong>\u8f93\u51fa\uff1a<\/strong> [2,3,4]<br \/><strong>\u8bf4\u660e\uff1a<\/strong><br \/> const newarray = map(arr, plusone); \/\/ [2,3,4]<br \/> \u8be5\u51fd\u6570\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u503c\u52a0\u4e00\u3002<\/p>\n<p><strong>\u793a\u4f8b2\uff1a<\/strong><\/p>\n<p><strong>\u8f93\u5165\uff1a<\/strong> arr = [1,2,3], fn = function plusi(n, i) { return n + i; }<br \/><strong>\u8f93\u51fa\uff1a<\/strong> [1,3,5]<br \/><strong>\u8bf4\u660e\uff1a<\/strong> \u8be5\u51fd\u6570\u5c06\u6bcf\u4e2a\u503c\u589e\u52a0\u5b83\u6240\u5728\u7684\u7d22\u5f15\u3002<\/p>\n<p><strong>\u793a\u4f8b3\uff1a<\/strong><\/p>\n<p><strong>\u8f93\u5165\uff1a<\/strong> arr = [10,20,30], fn = function constant() { return 42; }<br \/><strong>\u8f93\u51fa\uff1a<\/strong> [42,42,42]<br \/><strong>\u8bf4\u660e\uff1a<\/strong> \u8be5\u51fd\u6570\u59cb\u7ec8\u8fd4\u56de 42\u3002<\/p>\n<p><strong>\u9650\u5236\uff1a<\/strong><\/p>\n<ul>\n<li>0 &lt;=arr.length &lt;=1000<\/li>\n<li>-10^9 &lt;= arr[i] &lt;= 10^9<\/li>\n<li> fn \u8fd4\u56de\u4e00\u4e2a\u6570\u5b57<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<pre>\/**\n * @param {number[]} arr\n * @param {Function} fn\n * @return {number[]}\n *\/\nvar map = function (arr, fn) {\n    let newArr = [];\n\n    for (let i = 0; i &lt; arr.length; i++) {\n        newArr[i] = fn(arr[i], i);\n    }\n    return newArr;\n};\n<\/pre>\n<p><strong>\u4ee3\u7801\u6f14\u7ec3<\/strong><\/p>\n<p>var map = function (arr, fn) {<br \/> \u8fd9\u662f\u51fd\u6570\u58f0\u660e\u3002 map \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1aarr\uff08\u6570\u5b57\u6570\u7ec4\uff09\u548c fn\uff08\u5c06\u5e94\u7528\u4e8e\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u7684\u51fd\u6570\uff09\u3002<\/p>\n<p>\u8ba9 newarr = [];<br \/> \u521b\u5efa\u4e00\u4e2a\u7a7a\u6570\u7ec4\u6765\u5b58\u50a8\u8f6c\u6362\u540e\u7684\u6570\u7ec4\u5143\u7d20\u3002<\/p>\n<p>for (\u8ba9 i = 0; i &lt; arr.length; i++)<br \/> \u5faa\u73af\u904d\u5386\u6574\u4e2a\u8f93\u5165\u6570\u7ec4 arr <\/p>\n<p>newarr[i] = fn(arr[i], i);<br \/> \u68c0\u67e5 arr[i] \u4e2d\u7684\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u662f\u5426\u6ee1\u8db3\u6307\u5b9a\u6761\u4ef6\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5230\u7a7a\u6570\u7ec4 newarr<\/p>\n<p>\u8fd4\u56denewarr\uff1b<br \/> \u8fd4\u56de\u5305\u542b\u8f6c\u6362\u540e\u7684\u5143\u7d20\u7684\u65b0\u521b\u5efa\u7684\u6570\u7ec4\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\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   PHP\u6570\u5b57\u8fc7\u957f\u53d8\u4e3a\u79d1\u5b66\u8ba1\u6570\u6cd5\uff0c\u5982\u4f55\u6062\u590d\u539f\u6837\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362 \u6536\u85cf \u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u7c73\u4e91\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u300a#eetcode \u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u53d8\u6362\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01 \u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 arr \u548c\u4e00\u4e2a\u6620\u5c04\u51fd\u6570 fn\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u8f6c\u6362\u3002 \u8fd4\u56de\u7684\u6570\u7ec4\u5e94\u521b\u5efa\u4e3a returnedarray[i] = fn(arr[i], i). \u8bf7\u5728\u4e0d\u4f7f\u7528\u5185\u7f6earray.map\u65b9\u6cd5\u7684\u60c5\u51b5\u4e0b\u89e3\u51b3\u3002 \u793a\u4f8b1\uff1a \u8f93\u5165\uff1a arr = [1,2,3], fn = function plusone(n) { return n + 1; }\u8f93\u51fa\uff1a [2,3,4]\u8bf4\u660e\uff1a const newarray = map(arr, plusone); \/\/ [2,3,4] \u8be5\u51fd\u6570\u5c06\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u503c\u52a0\u4e00\u3002 \u793a\u4f8b2\uff1a \u8f93\u5165\uff1a arr = [1,2,3], fn = function plusi(n, i) { return n + i; }\u8f93\u51fa\uff1a [&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-53671","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53671","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=53671"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53671\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}