{"id":207150,"date":"2025-07-08T08:54:04","date_gmt":"2025-07-08T00:54:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207150\/"},"modified":"2025-07-08T08:54:04","modified_gmt":"2025-07-08T00:54:04","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e6%88%91%e7%9a%84%e8%8c%83%e5%9b%b4%e5%9c%a8%e5%88%87%e7%89%87%e4%b8%ad%e6%98%be%e7%a4%ba%e4%b8%8d%e5%ad%98%e5%9c%a8%e7%9a%84%e5%80%bc%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207150\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u6211\u7684\u8303\u56f4\u5728\u5207\u7247\u4e2d\u663e\u793a\u4e0d\u5b58\u5728\u7684\u503c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4e3a\u4ec0\u4e48\u6211\u7684\u8303\u56f4\u5728\u5207\u7247\u4e2d\u663e\u793a\u4e0d\u5b58\u5728\u7684\u503c\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 13:42:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4e3a\u4ec0\u4e48\u6211\u7684\u8303\u56f4\u5728\u5207\u7247\u4e2d\u663e\u793a\u4e0d\u5b58\u5728\u7684\u503c\uff1f\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u8bd5\u56fe\u7528 go \u89e3\u51b3 leetcode \u95ee\u9898\u3002\u95ee\u9898\u662f\u5b50\u96c6\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u6b63\u5728\u7f16\u5199\u7684\u5b8c\u6574\u4ee3\u7801\u4ee5\u53ca\u4e00\u4e9b\u8c03\u8bd5\u65e5\u5fd7\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    v := []int{9, 0, 3, 5, 7}\n    fmt.println(subsets(v))\n}\n\nfunc subsets(nums []int) [][]int {\n    result := [][]int{\n        []int{}, \/\/ empty\n    }\n\n    for _, num := range nums {  \n        fmt.println(\"==========\")\n        fmt.println(num)\n        fmt.printf(\"result = %v\\n\", result)\n\n        temp := [][]int{}\n        for _, r := range result {\n            fmt.printf(\"r = %v\\n\", r)\n            temp = append(temp, append(r, num))\n        }\n\n        for _, t := range temp {\n            result = append(result, t)\n        }\n\n        fmt.println(\"==========\")       \n    }\n\n    return result\n}<\/pre>\n<p>\uff08\u6211\u8fd8\u51c6\u5907\u4e86go\u6e38\u4e50\u573a\u7f51\u5740\uff09<\/p>\n<p>\u8fd9\u662f\u4e0a\u9762\u4ee3\u7801\u7684\u8f93\u51fa\uff1a<\/p>\n<pre>==========\n9\nresult = [[]]\nr = []\n==========\n==========\n0\nresult = [[] [9]]\nr = []\nr = [9]\n==========\n==========\n3\nresult = [[] [9] [0] [9 0]]\nr = []\nr = [9]\nr = [0]\nr = [9 0]\n==========\n==========\n5\nresult = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3]]\nr = []\nr = [9]\nr = [0]\nr = [9 0]\nr = [3]\nr = [9 3]\nr = [0 3]\nr = [9 0 3]\n==========\n==========\n7\nresult = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 5]] \/\/ (a)\nr = []\nr = [9]\nr = [0]\nr = [9 0]\nr = [3]\nr = [9 3]\nr = [0 3]\nr = [9 0 3]\nr = [5]\nr = [9 5]\nr = [0 5]\nr = [9 0 5]\nr = [3 5]\nr = [9 3 5]\nr = [0 3 5]\nr = [9 0 3 7] \/\/ (b)\n==========\n[[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 7] [7] [9 7] [0 7] [9 0 7] [3 7] [9 3 7] [0 3 7] [9 0 3 7] [5 7] [9 5 7] [0 5 7] [9 0 5 7] [3 5 7] [9 3 5 7] [0 3 5 7] [9 0 3 7 7]]<\/pre>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u7b2c\u4e94\u4e2a <code>result<\/code>\u3002 \uff08\u6211\u5728\u90a3\u91cc\u6307\u51fa\u4e86 <code>(a)<\/code> \uff09\u5230\u76ee\u524d\u4e3a\u6b62\uff0c <code>result<\/code> \u7684\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u662f <code>[9, 0, 3, 5]<\/code> \uff0c\u8fd9\u662f\u9884\u671f\u7684\u884c\u4e3a\u3002 \u4f46\u662f\uff0c\u4e4b\u540e\uff0c\u5f53\u6211\u5c1d\u8bd5\u5728 <code>result<\/code> \u4e2d\u5199\u5165\u8be5\u5143\u7d20\u7684\u8c03\u8bd5\u65e5\u5fd7\u65f6\uff0c\u5b83\u4f1a\u66f4\u6539\u4e3a <code>[9, 0, 3, 7]<\/code> ( <code>(b)<\/code> )\u3002<\/p>\n<p>\u4f60\u77e5\u9053\u4e3a\u4ec0\u4e48\u5417\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p><code>append<\/code> \u5728\u9700\u8981\u65f6\u66f4\u6539 <code>r<\/code>\uff1a<br \/> \u800c\u4e0d\u662f\uff1a<\/p>\n<pre>temp = append(temp, append(r, num))<\/pre>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre>rr := make([]int, len(r))\n            copy(rr, r)\n            rr = append(rr, num)\n            temp = append(temp, rr)<\/pre>\n<p>\u4f60\u5c31\u53ef\u4ee5\u5f00\u59cb\u4e86\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n)\n\nfunc main() {\n    v := []int{9, 0, 3, 5, 7}\n    fmt.println(subsets(v))\n}\n\nfunc subsets(nums []int) [][]int {\n    result := [][]int{\n        []int{}, \/\/ empty\n    }\n\n    for _, num := range nums {\n        fmt.println(\"==========\")\n        fmt.println(num)\n        fmt.printf(\"result = %v\\n\", result)\n\n        temp := [][]int{}\n        for _, r := range result {\n            fmt.printf(\"r = %v\\n\", r)\n            \/\/ append(r, num)\n            rr := make([]int, len(r))\n            copy(rr, r)\n            rr = append(rr, num)\n            temp = append(temp, rr)\n        }\n\n        for _, t := range temp {\n            result = append(result, t)\n        }\n\n        fmt.println(\"==========\")\n    }\n\n    return result\n}<\/pre>\n<p>\u53ea\u9700\u8c03\u8bd5\u60a8\u7684\u4ee3\u7801\uff0c\u60a8\u5c31\u4f1a\u770b\u5230\uff0c\u5b83\u53d1\u751f\u5728\u8fd9\u91cc\uff1a<\/p>\n<pre>temp := [][]int{}\n        for _, r := range result {\n            fmt.println(\"result =\", result, len(result), cap(result))\n            fmt.println(\"r ==\", r, len(r), cap(r))\n            fmt.println(\"num =\", num)\n            rr := append(r, num)\n            fmt.println(\"r ==\", r, len(r), cap(r))\n            fmt.println(\"rr ==\", rr, len(rr), cap(rr))\n            fmt.println(\"result =\", result, len(result), cap(result))\n            fmt.println(\"temp =\", temp)\n            temp = append(temp, rr)\n            fmt.println(\"temp =\", temp)\n        }<\/pre>\n<p>\u4ec0\u4e48\u65f6\u5019<\/p>\n<pre>result = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 5]] 16 16\nr == [9 0 3] 3 4\nnum = 7\nr == [9 0 3] 3 4\nrr == [9 0 3 7] 4 4\nresult = [[] [9] [0] [9 0] [3] [9 3] [0 3] [9 0 3] [5] [9 5] [0 5] [9 0 5] [3 5] [9 3 5] [0 3 5] [9 0 3 7]] 16 16<\/pre>\n<p>\u56e0\u4e3a\uff1a<\/p>\n<pre>append(r, num)<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4e3a\u4ec0\u4e48\u6211\u7684\u8303\u56f4\u5728\u5207\u7247\u4e2d\u663e\u793a\u4e0d\u5b58\u5728\u7684\u503c\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207150","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207150","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=207150"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207150\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207150"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}