{"id":203945,"date":"2025-05-22T08:56:38","date_gmt":"2025-05-22T00:56:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203945\/"},"modified":"2025-05-22T08:56:38","modified_gmt":"2025-05-22T00:56:38","slug":"%e7%94%b5%e6%ba%90%e7%bb%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203945\/","title":{"rendered":"\u7535\u6e90\u7ec4"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7535\u6e90\u7ec4<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u7535\u6e90\u7ec4\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241201\/1733042523674c215b8cc8c.jpg\" class=\"aligncenter\"><\/p>\n<p>\u95ee\u9898<\/p>\n<p>\u56de\u6eaf\u65b9\u6cd5\uff1a<br \/> tc:(2^n) \u5373\u6307\u6570\u65f6\u95f4\u590d\u6742\u5ea6\uff08\u56e0\u4e3a\u6211\u4eec\u5728\u6bcf\u6b21\u9012\u5f52\u8c03\u7528\u65f6\u90fd\u6709\u4e24\u4e2a\u9009\u62e9\uff0c\u5373\u8981\u4e48\u8003\u8651\u201cindex\u201d\u5904\u7684\u503c\uff0c\u8981\u4e48\u4e0d\u8003\u8651\u5bfc\u81f4 2 \u79cd\u53ef\u80fd\u7ed3\u679c\u7684\u503c\uff0c\u8fd9\u5c06\u53d1\u751f n \u6b21\uff09<br \/> sc:(2^n)*(n)\uff0cn \u8868\u793a\u4e34\u65f6 arraylist&lt;&gt;() \uff0c 2^n \u8868\u793a\u4e3b arraylist&lt;&gt;();<\/p>\n<pre>class solution {\n    public list&lt;list&lt;integer&gt;&gt; subsets(int[] nums) {\n        list&lt;list&lt;integer&gt;&gt; list = new arraylist&lt;&gt;();\n        powerset(nums,0,list,new arraylist&lt;integer&gt;());\n        return list;\n    }\n    public void powerset(int [] nums, int index , list&lt;list&lt;integer&gt;&gt; list, list&lt;integer&gt; l){\n        \/\/base case\n        if(index ==nums.length){\n            list.add(new arraylist&lt;&gt;(l));\n            return;\n        }\n        \/\/take\n        l.add(nums[index]); \/\/consider the value at 'index'\n        powerset(nums,index+1,list,l);\n        \/\/dont take;\n        l.remove(l.size()-1);\/\/ don't consider the value at 'index'\n        powerset(nums,index+1,list,l);\n    }\n}\n<\/pre>\n<hr>\n<p>\u4f7f\u7528\u4f4d\u64cd\u4f5c\uff1a<br \/> tc\uff1ao(2^n)*n<br \/> sc\uff1ao(2^n)*n\uff0c\uff082^n \u8868\u793a\u4e3b\u5217\u8868\uff0cn \u8868\u793a\u5b50\u96c6\u5217\u8868\uff0c\u5e76\u4e0d\u662f\u6240\u6709\u5b50\u96c6\u7684\u5927\u5c0f\u90fd\u662f n\uff0c\u4f46\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u5047\u8bbe\u60c5\u51b5\u662f\u8fd9\u6837\uff09<\/p>\n<p><strong>\u5148\u51b3\u6761\u4ef6<\/strong>\uff1a\u68c0\u67e5\u7b2ci\u4f4d\u662f\u5426\u5df2\u8bbe\u7f6e\uff08\u6709\u5173\u66f4\u591a\u8be6\u7ec6\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u4f4d\u64cd\u4f5c\u63d0\u793a\u548c\u6280\u5de7\u9875\u9762\uff09<br \/> \u76f4\u89c9\uff1a<br \/> \u5982\u679c\u5168\u90fd\u6ca1\u6709\u3002\u5b50\u96c6\u8868\u793a\u4e3a\u4e8c\u8fdb\u5236\u503c\uff0c<br \/> \u4f8b\u5982\uff1a\u5982\u679c n = 3\uff0c\u5373\u6570\u7ec4\u4e2d\u6709 3 \u4e2a\u503c\u3002<br \/> \u5c06\u6709 2^n = 8 \u4e2a\u5b50\u96c6<br \/> 8\u4e2a\u5b50\u96c6\u4e5f\u53ef\u4ee5\u8868\u793a\u4e3a<\/p>\n<table>\n<thead>\n<tr>\n<th>index 2<\/th>\n<th>index 1<\/th>\n<th>index 0<\/th>\n<th>subset number<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6211\u4eec\u5c06\u8003\u8651\u5230\uff0c\u5982\u679c\u4f4d\u503c\u4e3a 1\uff0c\u5219\u5e94\u8003\u8651 nums[] \u4e2d\u7684\u7d22\u5f15\u503c\u6765\u5f62\u6210\u5b50\u96c6\u3002<br \/> \u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u591f\u521b\u5efa\u6240\u6709\u5b50\u96c6<\/p>\n<pre>\nclass Solution {\n    public List&lt;List&lt;Integer&gt;&gt; subsets(int[] nums) {\n        List&lt;List&lt;Integer&gt;&gt; list = new ArrayList&lt;&gt;();\n        int n = nums.length;\n        int noOfSubset = 1&lt;&lt;n; \/\/ this is nothing but 2^n, i.e if there are n elements in the array, they will form 2^n subsets\n\n        for(int num = 0;num&lt;noOfSubset;num++){ \/\/\/ all possible subsets numbers\n            List&lt;Integer&gt; l = new ArrayList&lt;&gt;();\n            for(int i =0;i&lt;n;i++){\/\/ for the given subset number find which index value to pick \n                if((num &amp; (1&lt;&lt;i))!=0) l.add(nums[i]); \n            }\n            list.add(l);\n        }\n        return list;\n    }\n}\n<\/pre>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u7535\u6e90\u7ec4\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7535\u6e90\u7ec4 \u6536\u85cf \u5bf9\u4e8e\u4e00\u4e2a\u6587\u7ae0\u5f00\u53d1\u8005&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203945","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203945"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203945\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}