{"id":3307,"date":"2018-07-26T08:17:47","date_gmt":"2018-07-26T15:17:47","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3307"},"modified":"2018-07-26T08:18:08","modified_gmt":"2018-07-26T15:18:08","slug":"leetcode-47-permutations-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-47-permutations-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 47. Permutations II"},"content":{"rendered":"<h1>Problem<\/h1>\n<p>Given a collection of numbers that might contain duplicates, return all possible unique permutations.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> [1,1,2]\r\n<strong>Output:<\/strong>\r\n[\r\n  [1,1,2],\r\n  [1,2,1],\r\n  [2,1,1]\r\n]<\/pre>\n<h1><strong>Solution<\/strong><\/h1>\n<p>Time complexity: O(n!)<\/p>\n<p>Space complexity: O(n + k)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 20 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; permuteUnique(vector&lt;int&gt;&amp; nums) {\r\n    sort(begin(nums), end(nums));\r\n    vector&lt;vector&lt;int&gt;&gt; ans;\r\n    vector&lt;int&gt; used(nums.size());\r\n    vector&lt;int&gt; cur;\r\n    dfs(nums, cur, used, ans);\r\n    return ans;\r\n  }\r\nprivate:\r\n  void dfs(const vector&lt;int&gt;&amp; nums, vector&lt;int&gt;&amp; cur, vector&lt;int&gt;&amp; used, vector&lt;vector&lt;int&gt;&gt;&amp; ans) {\r\n    if (cur.size() == nums.size()) {\r\n      ans.push_back(cur);\r\n      return;\r\n    }\r\n    for (int i = 0; i &lt; nums.size(); ++i) {\r\n      if (used[i]) continue;\r\n      \/\/ Same number can be only used once at each depth.\r\n      if (i &gt; 0 &amp;&amp; nums[i] == nums[i - 1] &amp;&amp; !used[i - 1]) continue;\r\n      used[i] = 1;\r\n      cur.push_back(nums[i]);\r\n      dfs(nums, cur, used, ans);\r\n      cur.pop_back();\r\n      used[i] = 0;\r\n    }\r\n  }\r\n};<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-784-letter-case-permutation\/\">\u82b1\u82b1\u9171 LeetCode 784. Letter Case Permutation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] Solution&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[21,33,177,121],"class_list":["post-3307","post","type-post","status-publish","format-standard","hentry","category-searching","tag-deduplication","tag-dfs","tag-medium","tag-permutation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3307"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3307\/revisions"}],"predecessor-version":[{"id":3309,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3307\/revisions\/3309"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}