{"id":207466,"date":"2025-07-08T13:22:22","date_gmt":"2025-07-08T05:22:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207466\/"},"modified":"2025-07-08T13:22:22","modified_gmt":"2025-07-08T05:22:22","slug":"%e8%bf%99%e6%ae%b5%e4%bb%a3%e7%a0%81%e4%bb%80%e4%b9%88%e6%97%b6%e5%80%99%e8%a6%86%e7%9b%96%e4%ba%86slice%e5%85%83%e7%b4%a0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207466\/","title":{"rendered":"\u8fd9\u6bb5\u4ee3\u7801\u4ec0\u4e48\u65f6\u5019\u8986\u76d6\u4e86slice\u5143\u7d20\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>\u8fd9\u6bb5\u4ee3\u7801\u4ec0\u4e48\u65f6\u5019\u8986\u76d6\u4e86slice\u5143\u7d20\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 20:27:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u8fd9\u6bb5\u4ee3\u7801\u4ec0\u4e48\u65f6\u5019\u8986\u76d6\u4e86slice\u5143\u7d20\uff1f\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u9644\u52a0\u5143\u7d20\u4e4b\u524d\uff0c\u6211\u8bb0\u5f55\u4e86\u6bcf\u4e2a\u5143\u7d20\u3002\u4f46\u7ed3\u679c\u770b\u8d77\u6765\u6709\u4e9b\u5143\u7d20\u88ab\u8986\u76d6\u4e86\u3002<\/p>\n<p>\u4e0d\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u88ab\u8986\u76d6\u3002<\/p>\n<pre>package main\n\nimport \"fmt\"\n\nfunc main() {\n    graph := [][]int{\n        []int{3, 1},\n        []int{4, 6, 7, 2, 5},\n        []int{4, 6, 3},\n        []int{6, 4},\n        []int{7, 6, 5},\n        []int{6},\n        []int{7},\n        []int{},\n    }\n\n    fmt.Println(allPathsSourceTarget(graph))\n}\n\nfunc allPathsSourceTarget(graph [][]int) [][]int {\n    n := len(graph) - 1\n    result := make([][]int, 0, 200)\n\n    var pathRecord func(target, path []int)\n    pathRecord = func(target, path []int) {\n        if (len(target) == 0) &amp;&amp; (path[len(path)-1] == n) {\n            fmt.Println(\"insert into\", path) \/\/ should end with 7\n            result = append(result, path)\n        }\n\n        for _, v := range target {\n            pathRecord(graph[v], append(path, v))\n        }\n    }\n\n    for _, v := range graph[0] {\n        pathRecord(graph[v], []int{0, v})\n    }\n\n    return result\n}<\/pre>\n<p>\u7ed3\u679c\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u5e94\u4ee5 7 \u7ed3\u5c3e\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u7684\u95ee\u9898\u4e0e\u6b64\u884c\u6709\u5173\uff1a<\/p>\n<pre>pathrecord(graph[v], append(path, v))<\/pre>\n<p>go \u662f\u5982\u6b64\u201c\u806a\u660e\u201d\uff0c\u6240\u4ee5\u4ed6\u8bd5\u56fe\u91cd\u7528\u76f8\u540c\u7684\u5207\u7247\u5206\u914d\u7684\u5185\u5b58\uff0c\u800c\u4f60\u5b9e\u9645\u4e0a\u6539\u53d8\u4e86\u5df2\u7ecf\u6dfb\u52a0\u5230\u7ed3\u679c\u4e2d\u7684\u8def\u5f84\u3002 \uff09\uff1a<\/p>\n<p>\u8bd5\u8bd5\u8fd9\u4e2a\uff1a<\/p>\n<pre>newpath = make([]int, len(path))\ncopy(newpath, path)\npathrecord(graph[v], append(newpath, v))<\/pre>\n<p>\u8fd9\u5bf9\u6211\u6709\u7528\u3002\u6211\u8ba4\u4e3a\u9644\u52a0\u548c\u5207\u7247\u5f88\u5947\u602a<\/p>\n<p>\u6211\u5047\u8bbe\u652f\u6301\u201c\u6b64\u9644\u52a0\u201d\u7684\u5185\u5b58\u88ab\u4f20\u9012\u5230\u9012\u5f52\u51fd\u6570\u4e2d\uff0c\u56e0\u4e3a\u5207\u7247\u7684\u4f5c\u7528\u7c7b\u4f3c\u4e8e\u6307\u9488<\/p>\n<p>\u7136\u540e\u4e0b\u6b21\u5b83\u662f\u76f8\u540c\u7684\u5185\u5b58\uff0c\u56e0\u6b64\u5b83\u4f1a\u88ab\u8986\u76d6<\/p>\n<p>\u56e0\u6b64\uff0c\u60a8\u9700\u8981\u5728\u6bcf\u6b21\u9012\u5f52\u65f6\u83b7\u53d6\u526f\u672c\u4ee5\u9632\u6b62\u5176\u8986\u76d6<\/p>\n<pre>pathRecord = func(target, path []int) {\n    if (len(target) == 0) &amp;&amp; (path[len(path)-1] == n) {\n        var c []int = make([]int, len(path))\n        copy(c, path)\n        \/\/fmt.Println(\"insert into\", payload) \/\/ should end with 7\n        result = append(result, c)\n    }\n\n    for _, v := range target {\n        pathRecord(graph[v], append(path, v)) \/\/this append\n    }\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u8fd9\u6bb5\u4ee3\u7801\u4ec0\u4e48\u65f6\u5019\u8986\u76d6\u4e86slice\u5143\u7d20\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-207466","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207466","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=207466"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207466\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}