{"id":5193,"date":"2019-05-15T21:07:53","date_gmt":"2019-05-16T04:07:53","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5193"},"modified":"2019-05-15T21:08:25","modified_gmt":"2019-05-16T04:08:25","slug":"leetcode-90-subsets-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-90-subsets-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 90. Subsets II"},"content":{"rendered":"\n<p>Given a collection of integers that might contain duplicates,&nbsp;<strong><em>nums<\/em><\/strong>, return all possible subsets (the power set).<\/p>\n\n\n\n<p><strong>Note:<\/strong>\u00a0The solution set must not contain duplicate subsets.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> [1,2,2]\n<strong>Output:<\/strong>\n[\n  [2],\n  [1],\n  [1,2,2],\n  [2,2],\n  [1,2],\n  []\n]<\/pre>\n\n\n\n<p><strong>Solution: DFS<\/strong><\/p>\n\n\n\n<p>The key to this problem is how to remove\/avoid duplicates efficiently.<\/p>\n\n\n\n<p>For the same depth, among the same numbers, only the first number can be used.<\/p>\n\n\n\n<p>Time complexity: O(2^n * n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<vector<int>> subsetsWithDup(vector<int>& nums) {\n    const int n = nums.size();\n    sort(begin(nums), end(nums));\n    vector<vector<int>> ans;\n    vector<int> cur;\n    function<void(int)> dfs = [&](int s) {\n      ans.push_back(cur);\n      if (cur.size() == n)\n        return;      \n      for (int i = s; i < n; ++i) {\n        if (i > s && nums[i] == nums[i - 1]) continue;\n        cur.push_back(nums[i]);\n        dfs(i + 1);\n        cur.pop_back();\n      }\n    };\n    dfs(0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a collection of integers that might contain duplicates,&nbsp;nums, return all possible subsets (the power set). Note:\u00a0The solution set must not contain duplicate subsets. Example:&#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":[33,177,42],"class_list":["post-5193","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5193","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=5193"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5193\/revisions"}],"predecessor-version":[{"id":5195,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5193\/revisions\/5195"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}