{"id":312441,"date":"2024-05-20T22:38:42","date_gmt":"2024-05-20T14:38:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/312441.html"},"modified":"2024-05-20T22:38:52","modified_gmt":"2024-05-20T14:38:52","slug":"%e4%bd%bf%e7%94%a8-javascript-%e6%80%8e%e4%b9%88%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e6%8e%92%e5%88%97%e7%bb%84%e5%90%88%e7%ae%97%e6%b3%95-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/312441.html","title":{"rendered":"\u4f7f\u7528 JavaScript \u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u6392\u5217\u7ec4\u5408\u7b97\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26233244\/6f142585-61ea-4012-a14c-bda53dd09fee.webp\" alt=\"\u4f7f\u7528 JavaScript \u600e\u4e48\u5b9e\u73b0\u4e00\u4e2a\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\" \/><\/p>\n<p><p>\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\u5728JavaScript\u4e2d\u53ef\u4ee5\u5229\u7528\u9012\u5f52\u3001\u5faa\u73af\u3001\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b49\u6280\u672f\u5b9e\u73b0\u3002<strong>\u6392\u5217\uff08Permutation\uff09\u7b97\u6cd5<\/strong> \u4e3b\u8981\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5e8f\u5217\u7684\u6240\u6709\u53ef\u80fd\u7684\u6392\u5e8f\u65b9\u5f0f\u3001<strong>\u7ec4\u5408\uff08Combination\uff09\u7b97\u6cd5<\/strong> \u5219\u7528\u4e8e\u751f\u6210\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u9009\u51fa\u4e00\u90e8\u5206\u5143\u7d20\u7684\u6240\u6709\u65b9\u5f0f\u3002\u5bf9\u4e8e\u6392\u5217\u7b97\u6cd5\uff0c\u6211\u4eec\u4e3b\u8981\u901a\u8fc7\u4ea4\u6362\u5143\u7d20\u7684\u4f4d\u7f6e\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u6392\u5217\u5e8f\u5217\uff1b\u800c\u7ec4\u5408\u7b97\u6cd5\uff0c\u5219\u662f\u901a\u8fc7\u9010\u6b65\u9009\u62e9\u5143\u7d20\u5e76\u5728\u5269\u4f59\u5143\u7d20\u4e2d\u7ee7\u7eed\u9009\u62e9\u76f4\u5230\u8fbe\u5230\u6240\u9700\u4e2a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u8be6\u7ec6\u5c55\u5f00\u63cf\u8ff0\u6392\u5217\u7b97\u6cd5\uff1a\u6392\u5217\u7b97\u6cd5\u53ef\u4ee5\u901a\u8fc7\u201c\u56de\u6eaf\u6cd5\u201d\u6765\u8fdb\u884c\u5b9e\u73b0\u3002\u4ee3\u7801\u5c06\u4ece\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u5f0f\u5c06\u6bcf\u4e2a\u4f4d\u7f6e\u7684\u5143\u7d20\u4e0e\u540e\u9762\u7684\u6240\u6709\u4f4d\u7f6e\u7684\u5143\u7d20\u8fdb\u884c\u4ea4\u6362\uff0c\u751f\u6210\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002\u6bcf\u4e00\u6b21\u4ea4\u6362\u540e\uff0c\u8fdb\u5165\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684\u9009\u9879\uff0c\u76f4\u5230\u6700\u540e\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u6b64\u65f6\u53ef\u4ee5\u8f93\u51fa\u6216\u8005\u8bb0\u5f55\u4e00\u4e2a\u6392\u5217\u7ed3\u679c\u3002\u5982\u6b64\u7ee7\u7eed\u9012\u5f52\u56de\u5230\u4e4b\u524d\u7684\u4f4d\u7f6e\uff0c\u64a4\u9500\u4e4b\u524d\u7684\u4ea4\u6362\u64cd\u4f5c\uff0c\u4ee5\u4fbf\u8fdb\u884c\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684\u4ea4\u6362\u64cd\u4f5c\uff0c\u8fd9\u6837\u5c31\u80fd\u591f\u4fdd\u8bc1\u83b7\u5f97\u6240\u6709\u4e0d\u91cd\u590d\u7684\u6392\u5217\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6392\u5217\u7b97\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0\u6392\u5217\u7b97\u6cd5\uff0c\u6211\u4eec\u5c06\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u4ea4\u6362\u6570\u7ec4\u5185\u4e24\u4e2a\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u7136\u540e\u4f7f\u7528\u53e6\u4e00\u4e2a\u51fd\u6570\u9012\u5f52\u5730\u8fdb\u884c\u4f4d\u7f6e\u4ea4\u6362\u4ee5\u5f97\u5230\u6240\u6709\u6392\u5217\u3002<\/p>\n<\/p>\n<p><p><strong>\u4ea4\u6362\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function swap(array, i, j) {<\/p>\n<p>  var temp = array[i];<\/p>\n<p>  array[i] = array[j];<\/p>\n<p>  array[j] = temp;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6392\u5217\u9012\u5f52\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function permute(array, start, end) {<\/p>\n<p>  if(start === end) {<\/p>\n<p>    console.log(array);<\/p>\n<p>    return;<\/p>\n<p>  }<\/p>\n<p>  for(let i = start; i &lt;= end; i++) {<\/p>\n<p>    swap(array, start, i);<\/p>\n<p>    permute(array, start + 1, end);<\/p>\n<p>    swap(array, start, i); \/\/ \u56de\u6eaf<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6267\u884c\u6392\u5217<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const numbers = [1, 2, 3];<\/p>\n<p>permute(numbers, 0, numbers.length - 1);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u7ec4\u5408\u7b97\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u7ec4\u5408\u7b97\u6cd5\u76f8\u6bd4\u6392\u5217\u7b97\u6cd5\uff0c\u5176\u4e0d\u8003\u8651\u5e8f\u5217\u7684\u987a\u5e8f\uff0c\u53ea\u8003\u8651\u9009\u53d6\u7684\u5143\u7d20\u7684\u7ec4\u5408\u3002\u540c\u6837\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u7684\u65b9\u5f0f\u6765\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><p><strong>\u7ec4\u5408\u9012\u5f52\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function combine(sourceArray, comboLength, startIndex, initialStuff, output) {<\/p>\n<p>  if(initialStuff.length &gt;= comboLength) {<\/p>\n<p>    output.push([...initialStuff]);<\/p>\n<p>    return;<\/p>\n<p>  }<\/p>\n<p>  for(let i = startIndex; i &lt; sourceArray.length; i++) {<\/p>\n<p>    initialStuff.push(sourceArray[i]);<\/p>\n<p>    combine(sourceArray, comboLength, i + 1, initialStuff, output);<\/p>\n<p>    initialStuff.pop(); \/\/ \u56de\u6eaf<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u6267\u884c\u7ec4\u5408<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const srcArray = [1, 2, 3, 4];<\/p>\n<p>const comboLength = 2;<\/p>\n<p>const output = [];<\/p>\n<p>combine(srcArray, comboLength, 0, [], output);<\/p>\n<p>console.log(output);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f18\u5316\u7b97\u6cd5\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5f53\u6240\u5904\u7406\u7684\u6570\u636e\u96c6\u8f83\u5927\u65f6\uff0c\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\u7684\u6027\u80fd\u4f1a\u53d8\u5f97\u5341\u5206\u5173\u952e\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u8003\u8651\u4f18\u5316\u7b97\u6cd5\u6765\u51cf\u5c11\u8fd0\u7b97\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u79cd\u53ef\u80fd\u7684\u4f18\u5316\u662f\u5728\u9012\u5f52\u51fd\u6570\u4e2d\u6dfb\u52a0\u526a\u679d\u6761\u4ef6\uff0c\u5728\u6392\u5217\u6216\u8005\u7ec4\u5408\u7684\u8fc7\u7a0b\u4e2d\u53ca\u65e9\u6392\u9664\u4e0d\u53ef\u80fd\u6216\u8005\u91cd\u590d\u7684\u60c5\u51b5\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u9012\u5f52\u7684\u6b21\u6570\u3002\u5bf9\u4e8e\u7ec4\u5408\u7b97\u6cd5\u6765\u8bf4\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6570\u5b66\u7684\u65b9\u5f0f\u76f4\u63a5\u8ba1\u7b97\u51fa\u6240\u9700\u7ec4\u5408\u6570\uff0c\u4ece\u800c\u907f\u514d\u5b8c\u5168\u4f9d\u8d56\u8ba1\u7b97\u673a\u679a\u4e3e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\u5728\u5f88\u591a\u9886\u57df\u90fd\u6709\u5b9e\u9645\u7684\u5e94\u7528\uff0c\u5982\u5bc6\u7801\u5b66\u3001\u6982\u7387\u8ba1\u7b97\u3001\u6e38\u620f<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>\u7684\u51b3\u7b56\u6811\u751f\u6210\u7b49\u3002\u638c\u63e1\u5b83\u4eec\u7684\u5b9e\u73b0\uff0c\u80fd\u5b9e\u9645\u5e94\u7528\u4e8e\u8fd9\u4e9b\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u4e2d\uff0c\u5e76\u4e14\u5e38\u5e38\u662f\u8fd9\u4e9b\u95ee\u9898\u6c42\u89e3\u7684\u57fa\u7840\u3002\u5728\u5904\u7406\u65f6\uff0c\u8981\u8003\u8651\u5230\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u548c\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u786e\u4fdd\u5b83\u4eec\u80fd\u6709\u6548\u5730\u5728\u76ee\u6807\u73af\u5883\u4e2d\u8fd0\u884c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0\u4e00\u4e2a\u6392\u5217\u7b97\u6cd5\uff1f<\/strong><\/p>\n<p>\u6392\u5217\u7b97\u6cd5\u662f\u4e00\u79cd\u786e\u5b9a\u5bf9\u8c61\u987a\u5e8f\u7684\u7b97\u6cd5\u3002\u8fd9\u91cc\u662f\u4e00\u4e2a\u4f7f\u7528 JavaScript \u5b9e\u73b0\u6392\u5217\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u8def\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u83b7\u53d6\u7ed9\u5b9a\u6570\u7ec4\u7684\u6240\u6709\u6392\u5217\u7ec4\u5408\u3002<\/li>\n<li>\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u6765\u751f\u6210\u6392\u5217\u7ec4\u5408\u3002<\/li>\n<li>\u6bcf\u6b21\u8fed\u4ee3\u65f6\uff0c\u56fa\u5b9a\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u5269\u4f59\u5143\u7d20\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u672c\u60c5\u51b5\uff08\u6570\u7ec4\u4e3a\u7a7a\uff09\u3002<\/li>\n<li>\u5728\u9012\u5f52\u65b9\u6cd5\u4e2d\uff0c\u4ea4\u6362\u6570\u7ec4\u5143\u7d20\u7684\u4f4d\u7f6e\uff0c\u4ee5\u751f\u6210\u4e0d\u540c\u7684\u6392\u5217\u7ec4\u5408\u3002<\/li>\n<li>\u8fd4\u56de\u6240\u6709\u751f\u6210\u7684\u6392\u5217\u7ec4\u5408\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0\u4e00\u4e2a\u7ec4\u5408\u7b97\u6cd5\uff1f<\/strong><\/p>\n<p>\u7ec4\u5408\u7b97\u6cd5\u662f\u4e00\u79cd\u9009\u62e9\u67d0\u4e2a\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u7684\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4f7f\u7528 JavaScript \u5b9e\u73b0\u7ec4\u5408\u7b97\u6cd5\u7684\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u83b7\u53d6\u7ed9\u5b9a\u96c6\u5408\u7684\u6240\u6709\u7ec4\u5408\u3002<\/li>\n<li>\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u6765\u751f\u6210\u7ec4\u5408\u3002<\/li>\n<li>\u6bcf\u6b21\u8fed\u4ee3\u65f6\uff0c\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u5269\u4f59\u5143\u7d20\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u672c\u60c5\u51b5\uff08\u9700\u8981\u9009\u62e9\u7684\u5143\u7d20\u6570\u91cf\u4e3a0\uff09\u3002<\/li>\n<li>\u5728\u9012\u5f52\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u6765\u9009\u62e9\u4e0d\u540c\u7684\u5143\u7d20\uff0c\u5e76\u751f\u6210\u4e0d\u540c\u7684\u7ec4\u5408\u3002<\/li>\n<li>\u8fd4\u56de\u6240\u6709\u751f\u6210\u7684\u7ec4\u5408\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528 JavaScript \u5b9e\u73b0\u4e00\u4e2a\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\uff1f<\/strong><\/p>\n<p>\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\u662f\u6307\u540c\u65f6\u8003\u8651\u5143\u7d20\u7684\u6392\u5217\u548c\u7ec4\u5408\u7684\u7b97\u6cd5\u3002\u4e0b\u9762\u662f\u4f7f\u7528 JavaScript \u5b9e\u73b0\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\u7684\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u83b7\u53d6\u7ed9\u5b9a\u6570\u7ec4\u7684\u6240\u6709\u6392\u5217\u7ec4\u5408\u3002<\/li>\n<li>\u4f7f\u7528\u9012\u5f52\u65b9\u6cd5\u6765\u751f\u6210\u6392\u5217\u7ec4\u5408\u3002<\/li>\n<li>\u6bcf\u6b21\u8fed\u4ee3\u65f6\uff0c\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u5bf9\u5269\u4f59\u5143\u7d20\u8fdb\u884c\u9012\u5f52\u8c03\u7528\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u672c\u60c5\u51b5\uff08\u6570\u7ec4\u4e3a\u7a7a\uff09\u3002<\/li>\n<li>\u5728\u9012\u5f52\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u5faa\u73af\u6765\u9009\u62e9\u4e0d\u540c\u7684\u5143\u7d20\uff0c\u5e76\u751f\u6210\u4e0d\u540c\u7684\u6392\u5217\u7ec4\u5408\u3002<\/li>\n<li>\u8fd4\u56de\u6240\u6709\u751f\u6210\u7684\u6392\u5217\u7ec4\u5408\u6570\u7ec4\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u662f\u4e09\u79cd\u5b9e\u73b0\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\u7684\u65b9\u6cd5\uff0c\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"\u6392\u5217\u7ec4\u5408\u7b97\u6cd5\u5728JavaScript\u4e2d\u53ef\u4ee5\u5229\u7528\u9012\u5f52\u3001\u5faa\u73af\u3001\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\u7b49\u6280\u672f\u5b9e\u73b0\u3002\u6392\u5217\uff08Permutation\uff09 [&hellip;]","protected":false},"author":3,"featured_media":312457,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/312441"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=312441"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/312441\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/312457"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=312441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=312441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=312441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}