{"id":192257,"date":"2020-11-05T10:35:39","date_gmt":"2020-11-05T02:35:39","guid":{"rendered":"http:\/\/4563.org\/?p=192257"},"modified":"2020-11-05T10:35:39","modified_gmt":"2020-11-05T02:35:39","slug":"%e5%be%ae%e8%bd%af%e9%9d%a2%e8%af%95%e9%a2%98%ef%bc%9a%e5%85%a8%e6%8e%92%e5%88%97","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=192257","title":{"rendered":"\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u5168\u6392\u5217"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u5168\u6392\u5217               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zzzrf <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u5b57\u5217\u8868\uff0c\u8fd4\u56de\u5176\u6240\u6709\u53ef\u80fd\u7684\u6392\u5217\u3002<\/p>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740<\/p>\n<h2>\u6837\u4f8b 1\uff1a<\/h2>\n<pre><code>\u8f93\u5165\uff1a[1] \u8f93\u51fa\uff1a [   [1] ] <\/code><\/pre>\n<h2>\u6837\u4f8b 2\uff1a<\/h2>\n<pre><code>\u8f93\u5165\uff1a[1,2,3] \u8f93\u51fa\uff1a [   [1,2,3],   [1,3,2],   [2,1,3],   [2,3,1],   [3,1,2],   [3,2,1] ] <\/code><\/pre>\n<h2>\u7b97\u6cd5\uff1aDFS \uff08\u56de\u6eaf\u6cd5\uff09<\/h2>\n<h3>\u7b97\u6cd5\u5206\u6790<\/h3>\n<p>\u5bf9\u4e8e\u5168\u6392\u5217\uff0c\u6bd4\u5982[1,2,3]\u7684\u5168\u6392\u5217\uff0c\u6211\u4eec\u6309\u987a\u5e8f\u7a77\u4e3e\uff0c\u6b65\u9aa4\u5982\u4e0b\uff1a1. \u9996\u5148\u4f1a\u56fa\u5b9a\u7b2c\u4e00\u4f4d\u4e3a 1\uff0c\u7136\u540e\u7b2c\u4e8c\u4f4d\u5982\u679c\u53d6 2\uff0c\u6700\u540e\u4e00\u4f4d\u53ea\u80fd\u662f 3 \uff1b\u518d\u53ef\u4ee5\u628a\u7b2c\u4e8c\u4f4d\u53d8\u6210 3\uff0c\u7b2c\u4e09\u4f4d\u5c31\u53ea\u80fd\u662f 2 \uff1b 2. \u63a5\u4e0b\u6765\u53d8\u5316\u7b2c\u4e00\u4f4d\u53d8\u6210 2\uff0c\u7136\u540e\u518d\u50cf\u4e0a\u8ff0\u7684\u8fc7\u7a0b\u7a77\u4e3e\u540e\u4e24\u4f4d\u5206\u522b\u662f 1,3 \uff1b 3. \u6700\u540e\u7b2c\u4e00\u4f4d\u662f 3\uff0c\u518d\u7a77\u4e3e\u540e\u4e24\u4f4d\u5206\u522b\u662f 1,2\uff0c\u6574\u4e2a\u8fc7\u7a0b\u5c31\u5b8c\u6210\u4e86\u3002 \u4ee5\u4e0a\u7684\u8fc7\u7a0b\uff0c\u5f88\u9002\u5408\u4f7f\u7528 DFS \uff08\u56de\u6eaf\u6cd5\uff09\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<h3>\u7b97\u6cd5\u6b65\u9aa4<\/h3>\n<ol>\n<li>\u6839\u636e\u9898\u76ee\u8981\u6c42\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u5e03\u5c14\u7c7b\u578b used \u6570\u7ec4\u8bb0\u5f55\u6bcf\u4e2a\u6570\u5b57\u662f\u5426\u5df2\u88ab\u4f7f\u7528\uff0c\u4e00\u4e2a\u6570\u7ec4 current \u8bb0\u5f55\u5f53\u524d\u904d\u5386\u7684\u6392\u5217\uff0c\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 results \u8bb0\u5f55\u6240\u6709\u7ed3\u679c\uff1b<\/li>\n<li>\u5bf9\u8f93\u5165\u7684\u6570\u5b57\u5217\u8868 nums \u8fdb\u884c\u6df1\u5ea6\u4f18\u5148\u641c\u7d22\uff0c\u4f20\u5165\u7684\u53c2\u6570\u5305\u62ec\uff1a\u6570\u5b57\u5217\u8868 nums \u3001used \u3001current \u3001results \uff1b<\/li>\n<\/ol>\n<ul>\n<li>\u8fb9\u754c\u6761\u4ef6\uff1a\u5f53 current \u4e2d\u7684\u5143\u7d20\u4e2a\u6570\u548c nums \u7684\u76f8\u540c\uff0c\u4ee3\u8868\u4e00\u6b21\u904d\u5386\u5b8c\u6bd5\uff0c\u5c06\u5f53\u524d\u7684 current \u52a0\u5165 results \u5e76 return \uff1b<\/li>\n<li>\u904d\u5386 nums\uff0c\u5982\u679c nums \u4e2d\u5f53\u524d\u8fd9\u4e2a\u5143\u7d20\u672a\u88ab\u4f7f\u7528\uff0c\u5219\u5728 used \u4e2d\u6807\u8bb0\u5176\u5df2\u4f7f\u7528\uff0c\u5c06\u5176\u52a0\u5165 current\uff0c\u5e76\u9012\u5f52\u8c03\u7528 dfs \uff1b<\/li>\n<li>\u8c03\u7528\u5b8c\u6bd5\u540e\u9700\u8981\u56de\u9000\uff0c\u5373\u5728 used \u4e2d\u6807\u8bb0\u5176\u672a\u4f7f\u7528\uff1b<\/li>\n<\/ul>\n<h2>\u590d\u6742\u5ea6<\/h2>\n<ul>\n<li>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201115_5fb0fffe8ffae.png\" alt=\"\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u5168\u6392\u5217\" \/><\/p>\n<ul>\n<li>\u5bf9\u4e8e\u6bcf\u4e00\u4f4d\uff0c\u53ef\u4ee5\u4ece n \u4e2a\u5143\u7d20\u4e2d\u9009\u62e9 k \u4e2a\u6765\u653e\u7f6e\uff0c\u5171\u6709 n \u4f4d\u3002<\/li>\n<li>\u7a7a\u95f4\u590d\u6742\u5ea6\uff1aO(N!)\n<ul>\n<li>\u5171\u6709 N!\u4e2a\u5168\u6392\u5217\uff0c\u6545\u9700\u8981\u4fdd\u5b58 N!\u4e2a\u89e3<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>import java.util.ArrayList;import java.util.List; public class Solution {     \/*      * @param nums: A list of integers.      *       * @return: A list of permutations.      *\/     public List&lt;List&lt;Integer&gt;&gt; permute(int[] nums) {         List&lt;List&lt;Integer&gt;&gt; results = new ArrayList&lt;&gt;();          \/\/ \u5982\u679c\u6570\u7ec4\u4e3a\u7a7a\u76f4\u63a5\u8fd4\u56de\u7a7a         if (nums == null) {             return results;         }          \/\/dfs         dfs(nums, new boolean[nums.length], new ArrayList&lt;Integer&gt;(), results);          return results;     }      private void dfs(int[] nums, boolean[] used, List&lt;Integer&gt; current, List&lt;List&lt;Integer&gt;&gt; results) {                  \/\/\u627e\u5230\u4e00\u7ec4\u6392\u5217\uff0c\u5df2\u5230\u8fbe\u8fb9\u754c\u6761\u4ef6         if (nums.length == current.size()) {             results.add(new ArrayList&lt;Integer&gt;(current));             return;         }          for (int i = 0; i &lt; nums.length; i++) {             \/\/i \u4f4d\u7f6e\u8fd9\u4e2a\u5143\u7d20\u5df2\u7ecf\u88ab\u7528\u8fc7             if (used[i]) {                 continue;             }              \/\/\u7ee7\u7eed\u9012\u5f52             current.add(nums[i]);             used[i] = true;             dfs(nums, used, current, results);             used[i] = false;             current.remove(current.size() - 1);         }     } } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u8f6f\u9762\u8bd5\u9898\uff1a\u5168\u6392\u5217 \u8cc7\u6df1\u5927\u4f6c : &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/192257"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192257"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/192257\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192257"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}