{"id":1060,"date":"2025-12-26T04:18:52","date_gmt":"2025-12-25T20:18:52","guid":{"rendered":"https:\/\/www.guanhaobo.cn\/?p=1060"},"modified":"2025-12-26T04:18:52","modified_gmt":"2025-12-25T20:18:52","slug":"leetcode-31-%e4%b8%8b%e4%b8%80%e4%b8%aa%e6%8e%92%e5%88%97","status":"publish","type":"post","link":"https:\/\/www.guanhaobo.cn\/?p=1060","title":{"rendered":"LeetCode 31 \u2013 \u4e0b\u4e00\u4e2a\u6392\u5217"},"content":{"rendered":"<h1>\u9898\u76ee\u63cf\u8ff0<\/h1>\n<p>\u6574\u6570\u6570\u7ec4\u7684\u4e00\u4e2a \u6392\u5217  \u5c31\u662f\u5c06\u5176\u6240\u6709\u6210\u5458\u4ee5\u5e8f\u5217\u6216\u7ebf\u6027\u987a\u5e8f\u6392\u5217\u3002<br \/>\n\u4f8b\u5982\uff0carr = [1,2,3] \uff0c\u4ee5\u4e0b\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u89c6\u4f5c arr \u7684\u6392\u5217\uff1a[1,2,3]\u3001[1,3,2]\u3001[3,1,2]\u3001[2,3,1] \u3002<br \/>\n\u6574\u6570\u6570\u7ec4\u7684 \u4e0b\u4e00\u4e2a\u6392\u5217 \u662f\u6307\u5176\u6574\u6570\u7684\u4e0b\u4e00\u4e2a\u5b57\u5178\u5e8f\u66f4\u5927\u7684\u6392\u5217\u3002\u66f4\u6b63\u5f0f\u5730\uff0c\u5982\u679c\u6570\u7ec4\u7684\u6240\u6709\u6392\u5217\u6839\u636e\u5176\u5b57\u5178\u987a\u5e8f\u4ece\u5c0f\u5230\u5927\u6392\u5217\u5728\u4e00\u4e2a\u5bb9\u5668\u4e2d\uff0c\u90a3\u4e48\u6570\u7ec4\u7684 \u4e0b\u4e00\u4e2a\u6392\u5217 \u5c31\u662f\u5728\u8fd9\u4e2a\u6709\u5e8f\u5bb9\u5668\u4e2d\u6392\u5728\u5b83\u540e\u9762\u7684\u90a3\u4e2a\u6392\u5217\u3002\u5982\u679c\u4e0d\u5b58\u5728\u4e0b\u4e00\u4e2a\u66f4\u5927\u7684\u6392\u5217\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6570\u7ec4\u5fc5\u987b\u91cd\u6392\u4e3a\u5b57\u5178\u5e8f\u6700\u5c0f\u7684\u6392\u5217\uff08\u5373\uff0c\u5176\u5143\u7d20\u6309\u5347\u5e8f\u6392\u5217\uff09\u3002<br \/>\n\u4f8b\u5982\uff0carr = [1,2,3] \u7684\u4e0b\u4e00\u4e2a\u6392\u5217\u662f [1,3,2] \u3002<br \/>\n\u7c7b\u4f3c\u5730\uff0carr = [2,3,1] \u7684\u4e0b\u4e00\u4e2a\u6392\u5217\u662f [3,1,2] \u3002<br \/>\n\u800c arr = [3,2,1] \u7684\u4e0b\u4e00\u4e2a\u6392\u5217\u662f [1,2,3] \uff0c\u56e0\u4e3a [3,2,1] \u4e0d\u5b58\u5728\u4e00\u4e2a\u5b57\u5178\u5e8f\u66f4\u5927\u7684\u6392\u5217\u3002<br \/>\n\u7ed9\u4f60\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u627e\u51fa nums \u7684\u4e0b\u4e00\u4e2a\u6392\u5217\u3002<br \/>\n\u5fc5\u987b \u539f\u5730 \u4fee\u6539\uff0c\u53ea\u5141\u8bb8\u4f7f\u7528\u989d\u5916\u5e38\u6570\u7a7a\u95f4\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<br \/>\n\u8f93\u5165\uff1anums = [1,2,3]<br \/>\n\u8f93\u51fa\uff1a[1,3,2]<\/p>\n<h1>\u9898\u76ee\u5206\u6790<\/h1>\n<p>\u4ee5 [1, 4, 6, 5, 3, 2] \u4e3a\u4f8b\uff1a<br \/>\n1. \u4ece\u540e\u5411\u524d\u904d\u5386\uff0c\u627e\u5230\u7b2c\u4e00\u4e2a\u5c0f\u4e8e\u540e\u9762\u503c\u7684\u6570\u5b57 4\uff0c\u8bbe\u4e0b\u6807\u4e3aindex\uff0cindex \u540e\u9762\u7684\u6570\u5b57\u662f\u9012\u51cf\u7684<br \/>\n2. \u4ece\u540e\u5411\u524d\u904d\u5386\uff0c\u627e\u5230\u7b2c\u4e00\u4e2a\u5927\u4e8e4\u7684\u6570\u5b575\uff0c\u5c064\u548c5\u4ea4\u6362\u4f4d\u7f6e [1, 5, 6, 4, 3, 2]\uff0c\u4ea4\u6362\u540e\uff0cindex \u540e\u9762\u4f9d\u7136\u662f\u9012\u51cf\u7684<br \/>\n3. \u5c06 index \u540e\u53cd\u8f6c\uff0c\u4ece\u9012\u51cf\u53d8\u6210\u9012\u589e  [1, 5, 2, 3, 4, 6]<\/p>\n<p>\u65f6\u95f4\u590d\u6742\u5ea6 O(n)<\/p>\n<h1>Java<\/h1>\n<pre><code class=\"language-java line-numbers\">public void nextPermutation(int[] nums) {\n    int index = nums.length - 2;\n    while (index &gt;= 0) {\n        if (nums[index] &lt; nums[index + 1]) {\n            for (int i = nums.length - 1; i &gt; index; i--) {\n                if (nums[i] &gt; nums[index]) {\n                    swap(nums, i, index);\n                    break;\n                }\n            }\n\n            break;\n        }\n        index--;\n    }\n\n    int left = index + 1, right = nums.length - 1;\n    while (left &lt; right) {\n        swap(nums, left++, right--);\n    }\n\n}\n\nprivate void swap(int[] nums, int i, int j) {\n    int temp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = temp;\n}\n<\/code><\/pre>\n<h1>Kotlin<\/h1>\n<pre><code class=\"language-kotlin line-numbers\">fun nextPermutation(nums: IntArray): Unit {\n    var index = nums.size - 2\n    while (index &gt;= 0) {\n        if (nums[index] &lt; nums[index + 1]) {\n            for (i in nums.indices.reversed()) {\n                if (nums[i] &gt; nums[index]) {\n                    swap(nums, i, index)\n                    break\n                }\n            }\n            break\n        }\n        index--\n    }\n\n    var left = index + 1\n    var right = nums.size - 1\n    while (left &lt; right) {\n        swap(nums, left++, right--)\n    }\n}\n\nprivate fun swap(nums: IntArray, i: Int, j: Int) {\n    val temp = nums[i]\n    nums[i] = nums[j]\n    nums[j] = temp\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u6574\u6570\u6570\u7ec4\u7684\u4e00\u4e2a \u6392\u5217 \u5c31\u662f\u5c06\u5176\u6240\u6709\u6210\u5458\u4ee5\u5e8f\u5217\u6216\u7ebf\u6027\u987a\u5e8f\u6392\u5217\u3002 \u4f8b\u5982\uff0carr = [1,2,3] \uff0c\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20,92,73],"class_list":["post-1060","post","type-post","status-publish","format-standard","hentry","category-algo","tag-leetcode","tag-92","tag-73"],"_links":{"self":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1060"}],"version-history":[{"count":1,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions"}],"predecessor-version":[{"id":1061,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=\/wp\/v2\/posts\/1060\/revisions\/1061"}],"wp:attachment":[{"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guanhaobo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}