{"id":102,"date":"2023-12-25T13:51:44","date_gmt":"2023-12-25T05:51:44","guid":{"rendered":"http:\/\/119.23.58.79\/?p=102"},"modified":"2023-12-26T12:26:21","modified_gmt":"2023-12-26T04:26:21","slug":"%e5%9b%9e%e6%ba%af%e6%b3%951","status":"publish","type":"post","link":"https:\/\/wangqianming.top\/index.php\/2023\/12\/25\/%e5%9b%9e%e6%ba%af%e6%b3%951\/","title":{"rendered":"\u56de\u6eaf\u6cd51"},"content":{"rendered":"<h3>\u4eca\u5929leetcode\u4e0a\u5237\u5230\u4e00\u9898\uff0c\u540d\u53eb \u5168\u6392\u5217<\/h3>\n<p>\u5dee\u4e0d\u591a\u5c31\u662f\u8f93\u51fa\u4e00\u4e2a\u6570\u7ec4\u7684\u6240\u6709\u6392\u5217\u60c5\u51b5<br \/>\n\u5982 input: <code>[1,2]<\/code><br \/>\noutput: <code>[1,2],[2,1]<\/code><br \/>\n\u5b98\u65b9\u7b54\u6848\u5199\u7684\u5f88\u597d\uff0c\u7801\u4e00\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">class Solution {\n    public List&lt;List&lt;Integer&gt;&gt; permute(int[] nums) {\n        List&lt;List&lt;Integer&gt;&gt; res = new ArrayList&lt;List&lt;Integer&gt;&gt;();\n\n        List&lt;Integer&gt; output = new ArrayList&lt;Integer&gt;();\n        for (int num : nums) {\n            output.add(num);\n        }\n\n        int n = nums.length;\n        backtrack(n, output, res, 0);\n        return res;\n    }\n\n    public void backtrack(int n, List&lt;Integer&gt; output, List&lt;List&lt;Integer&gt;&gt; res, int first) {\n        \/\/ \u6240\u6709\u6570\u90fd\u586b\u5b8c\u4e86\n        if (first == n) {\n            res.add(new ArrayList&lt;Integer&gt;(output));\n        }\n        for (int i = first; i &lt; n; i++) {\n            \/\/ \u52a8\u6001\u7ef4\u62a4\u6570\u7ec4\n            Collections.swap(output, first, i);\n            \/\/ \u7ee7\u7eed\u9012\u5f52\u586b\u4e0b\u4e00\u4e2a\u6570\n            backtrack(n, output, res, first + 1);\n            \/\/ \u64a4\u9500\u64cd\u4f5c\n            Collections.swap(output, first, i);\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u5176\u4e2d\u7684backtrack\u5c31\u662f\u56de\u6eaf\u6cd5\u7684\u6807\u51c6\u65b9\u6cd5\uff0c\u5176\u4e2d\u4f7f\u7528\u4e86 <code>Collections.swap(output, first, i);<\/code> \u611f\u89c9\u4ee5\u540e\u53ef\u4ee5\u7528\u5230\u3002 <\/p>\n<blockquote>\n<p>\u539f\u9898\u94fe\u63a5\uff1a <code>https:\/\/leetcode.cn\/problems\/permutations\/<\/code><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929leetcode\u4e0a\u5237\u5230\u4e00\u9898\uff0c\u540d\u53eb \u5168\u6392\u5217 \u5dee\u4e0d\u591a\u5c31\u662f\u8f93\u51fa\u4e00\u4e2a\u6570\u7ec4\u7684\u6240\u6709\u6392\u5217\u60c5\u51b5 \u5982 input: [1,2] output:  &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,9],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-java","tag-leetcode"],"_links":{"self":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":7,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions\/114"}],"wp:attachment":[{"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wangqianming.top\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}