{"id":2192,"date":"2016-09-16T16:28:03","date_gmt":"2016-09-16T08:28:03","guid":{"rendered":"http:\/\/boweihe.me\/?p=2192"},"modified":"2016-09-16T16:28:03","modified_gmt":"2016-09-16T08:28:03","slug":"leetcode-78-subsets","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=2192","title":{"rendered":"LeetCode 78. Subsets"},"content":{"rendered":"<h3>\u601d\u8def<\/h3>\n<p>\u6362\u4e2a\u65b9\u6cd5\u60f3\u95ee\u9898\u3002<br \/>\n\u6709n\u4e2a\u6570\uff0c\u6bcf\u4e2a\u6570\u662f\u5426\u4f5c\u4e3a\u8f93\u51fa\u75280\/1\u72b6\u6001\u6765\u8868\u793a\uff0c\u6bd4\u5982[1,2,3]\u5168\u51fa\u73b0\u5c31\u662f(1,1,1) =&gt; 7\uff0c\u90a3n\u4e2a\u5143\u7d20\u7684\u5b50\u6570\u7ec4\u7684\u751f\u6210\u5c31\u662f\u4ee5\u4e0b\u8fc7\u7a0b\uff1a[0, 2^n)\u91cc\u7684\u6bcf\u4e2a\u6570i\uff0c\u770b\u8fd9\u4e2a\u6570i\u91cc\u7b2c[0,n)\u4f4d\u662f\u5426\u4e3a1\uff0c\u82e5\u4e3a1\u5219\u8868\u793anums[i]\u88ab\u9009\u4e2d\u4e86\uff0c\u5426\u5219\u4e0d\u9009\u3002<br \/>\n\u6bd4\u5982[1,2,3]\u7684\u6240\u6709\u5b50\u96c6:<\/p>\n<pre class=\"lang:default decode:true \">[               [1, 2, 3]\n  [3],        =&gt; 0, 0, 1  =&gt; 1\n  [1],        =&gt; 1, 0, 0  =&gt; 4\n  [2],        =&gt; 0, 1, 0  =&gt; 2\n  [1,2,3],    =&gt; 1, 1, 1  =&gt; 7\n  [1,3],      =&gt; 1, 0, 1  =&gt; 5\n  [2,3],      =&gt; 0, 1, 1  =&gt; 3\n  [1,2],      =&gt; 1, 1, 0  =&gt; 6\n  []          =&gt; 0, 0, 0  =&gt; 0\n]<\/pre>\n<h3>\u4ee3\u7801<\/h3>\n<pre class=\"lang:c++ decode:true \">class Solution {\npublic:\n    vector&lt;int&gt; decodeMask(unsigned int mask, const vector&lt;int&gt;&amp; nums){\n        vector&lt;int&gt; result;\n        unsigned int tester = 0x1;\n        for(unsigned int i=0; i&lt;nums.size(); i++){\n            if(mask &amp; tester){\n                result.push_back(nums[i]);\n            }\n            tester &lt;&lt;= 1;\n        }\n        return result;\n    }\n    vector&lt;vector&lt;int&gt;&gt; subsets(vector&lt;int&gt;&amp; nums) {\n        vector&lt;vector&lt;int&gt;&gt; results;\n        const unsigned int MAX_MASK = pow(2, nums.size());\n        for(unsigned int mask=0; mask &lt; MAX_MASK; mask++){\n            results.push_back(decodeMask(mask, nums));\n        }\n        return results;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8def \u6362\u4e2a\u65b9\u6cd5\u60f3\u95ee\u9898\u3002 \u6709n\u4e2a\u6570\uff0c\u6bcf\u4e2a\u6570\u662f\u5426\u4f5c\u4e3a\u8f93\u51fa\u75280\/1\u72b6\u6001\u6765\u8868\u793a\uff0c\u6bd4\u5982[1,2,3]\u5168\u51fa\u73b0\u5c31\u662f(1,1,1) =&gt; 7\uff0c\u90a3n\u4e2a\u5143\u7d20\u7684\u5b50\u6570\u7ec4\u7684\u751f\u6210\u5c31\u662f\u4ee5\u4e0b\u8fc7\u7a0b\uff1a[0, 2^n)\u91cc\u7684\u6bcf\u4e2a\u6570i\uff0c\u770b\u8fd9\u4e2a\u6570i\u91cc\u7b2c[0,n)\u4f4d\u662f\u5426\u4e3a1\uff0c\u82e5\u4e3a1\u5219\u8868\u793anums[i]\u88ab\u9009\u4e2d\u4e86\uff0c\u5426\u5219\u4e0d\u9009\u3002 \u6bd4\u5982[1,2,3]\u7684\u6240\u6709\u5b50\u96c6: [ [1, 2, 3] [3], =&gt; 0, 0, 1 =&gt; 1 [1], =&gt; 1, 0, 0 =&gt; 4 [2], =&gt; 0, 1, 0 =&gt; 2 [1,2,3], =&gt; 1, 1, 1 =&gt; 7 [1,3], =&gt; 1, 0, 1 =&gt; 5 [2,3], =&gt; 0, 1, 1 =&gt; 3 [1,2], =&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66,158],"class_list":["post-2192","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj","tag-158"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2192"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/2192\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}