{"id":4521,"date":"2018-12-22T09:58:12","date_gmt":"2018-12-22T17:58:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4521"},"modified":"2018-12-22T12:13:18","modified_gmt":"2018-12-22T20:13:18","slug":"leetcode-78-subsets","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-78-subsets\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 78. Subsets"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 78. Subsets - \u5237\u9898\u627e\u5de5\u4f5c EP236\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/CUzm-buvH_8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given a set of&nbsp;<strong>distinct<\/strong>&nbsp;integers,&nbsp;<em>nums<\/em>, return all possible subsets (the power set).<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The 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 crayon:false\"><strong>Input:<\/strong> nums = [1,2,3]<br><strong>Output:<\/strong>[  [3],&nbsp; [1],&nbsp; [2],&nbsp; [1,2,3],&nbsp; [1,3],&nbsp; [2,3],&nbsp; [1,2],&nbsp; []]<\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Solution: Combination<\/strong><\/h1>\n\n\n\n<p>Time complexity: O(2^n)<br>Space complexity: O(n)<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/78-ep236.png\" alt=\"\" class=\"wp-image-4530\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/78-ep236.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/78-ep236-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/78-ep236-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Implemention 1: DFS<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 4 ms\nclass Solution {\npublic:\n  vector<vector<int>> subsets(vector<int>& nums) {\n    vector<vector<int>> ans;\n    vector<int> cur;\n    for (int i = 0; i <= nums.size(); ++i)\n      dfs(nums, i, 0, cur, ans);\n    return ans;\n  }\nprivate:  \n  void dfs(const vector<int>& nums, int n, int s, \n           vector<int>& cur, vector<vector<int>>& ans) {\n    if (n == cur.size()) {\n      ans.push_back(cur);\n      return;\n    }\n    for (int i = s; i < nums.size(); ++i) {\n      cur.push_back(nums[i]);\n      dfs(nums, n, i + 1, cur, ans);\n      cur.pop_back();\n    }\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n# Author: Huahua, running time: 60 ms\nclass Solution:\n  def subsets(self, nums):\n    ans = []\n    def dfs(n, s, cur):\n      if n == len(cur):\n        ans.append(cur.copy())\n        return\n      for i in range(s, len(nums)):\n        cur.append(nums[i])\n        dfs(n, i + 1, cur)\n        cur.pop()\n    for i in range(len(nums) + 1):\n      dfs(i, 0, [])\n    return ans\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Implementation 2: Binary<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 4 ms\nclass Solution {\npublic:\n  vector<vector<int>> subsets(vector<int>& nums) {\n    const int n = nums.size();\n    vector<vector<int>> ans;    \n    for (int s = 0; s < 1 << n; ++s) {\n      vector<int> cur;\n      for (int i = 0; i < n; ++i)\n        if (s &#038; (1 << i)) cur.push_back(nums[i]);\n      ans.push_back(cur);\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n# Author: Huahua, 40 ms\nclass Solution:\n  def subsets(self, nums):\n    n = len(nums)\n    return [[nums[i] for i in range(n) if s & 1 << i > 0] for s in range(1 << n)]\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a set of&nbsp;distinct&nbsp;integers,&nbsp;nums, return all possible subsets (the power set). Note:&nbsp;The solution set must not contain duplicate subsets. Example: Input: nums = [1,2,3]Output:[ [3],&nbsp;&#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":[39,16,122,33,177,42],"class_list":["post-4521","post","type-post","status-publish","format-standard","hentry","category-searching","tag-binary","tag-bit","tag-combination","tag-dfs","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4521","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=4521"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4521\/revisions"}],"predecessor-version":[{"id":4532,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4521\/revisions\/4532"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}