{"id":188376,"date":"2020-11-07T22:05:57","date_gmt":"2020-11-07T14:05:57","guid":{"rendered":"http:\/\/4563.org\/?p=188376"},"modified":"2020-11-07T22:05:57","modified_gmt":"2020-11-07T14:05:57","slug":"golang-%e6%b1%82%e5%8a%a9%e5%a4%a7%e4%bd%ac","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=188376","title":{"rendered":"golang \u6c42\u52a9\u5927\u4f6c"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  golang \u6c42\u52a9\u5927\u4f6c               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : an93 <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>import &#8220;fmt&#8221;<\/p>\n<pre><code>func main() {     input := []int{2,3, 5}     target := 8     fmt.Println(combinationSum(input, target))  }  func combinationSum(candidates []int, target int) [][]int {    var res [][]int     var backtrack func(int, int, []int)    backtrack = func(idx int, sum int, nums []int) {         if sum == target {            res = append(res, nums)        }        if sum &gt;= target {            return        }        for i := idx; i &lt; len(candidates); i++ {            backtrack(i, sum + candidates[i], append(nums,candidates[i]))        }    }    backtrack(0, 0, []int{})    return res } <\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7ed3\u679c\u6709\u95ee\u9898\uff0c \u800c\u4e14\u95ee\u9898\u5904\u5728 res = append(res, nums) \u8fd9\u4e00\u884c\uff0c \u5982\u679c\u6211\u628a nums copy \u4e00\u4e2a\u518d append \u5c31\u4e0d\u4f1a\u6709\u95ee\u9898\u4e86\uff0c\u542c\u8bf4\u8fd9\u91cc\u5927\u795e\u6bd4\u8f83\u591a\uff0c\u6c42\u5927\u795e\u4eec\u6307\u6559\u4e00\u4e0b<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>3<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4095830\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : an93 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/play.studygolang.com\/p\/Smu8A5nw-rV                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4095831\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luguhu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             slice \u662f\u6307\u9488\u4f20\u9012                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4095832\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhouenxian <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u51fd\u6570\u4f20\u53c2\u6570\u786e\u5b9e\u662f\u503c\u4f20\u9012\uff0c\u4f46 slice \u4f20\u9012\u7684\u503c\u4e2d\u5305\u542b\u4e86\u5185\u5bb9\u7684\u5730\u5740\uff0c\u6240\u4ee5\u8fd8\u662f\u628a\u5730\u5740\u4f20\u4e86\u8fdb\u53bb\u3002<br \/>\u5982\u4e0b\u4e3a\u5207\u7247\u7c7b\u578b\u7684\u5185\u90e8\u5b9a\u4e49\u3002<br \/> type _slice struct {<br \/> elements unsafe.Pointer \/\/ \u5f15\u7528\u7740\u5e95\u5c42\u5b58\u50a8\u5728\u95f4\u63a5\u90e8\u5206\u4e0a\u7684\u5143\u7d20<br \/> len int \/\/ \u957f\u5ea6<br \/> cap int \/\/ \u5bb9\u91cf<br \/> }<br \/>\u5f15\u7528\uff1ago 101\uff0chttps:\/\/gfw.go101.org\/article\/container.html                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>golang \u6c42\u52a9\u5927\u4f6c \u8cc7\u6df1\u5927\u4f6c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/188376"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=188376"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/188376\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=188376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=188376"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=188376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}