{"id":2879,"date":"2020-02-04T15:03:06","date_gmt":"2020-02-04T07:03:06","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2879"},"modified":"2020-02-04T15:03:06","modified_gmt":"2020-02-04T07:03:06","slug":"%e3%80%90leetcode%e3%80%910039-%e7%bb%84%e5%90%88%e6%80%bb%e6%95%b0","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/2879\/","title":{"rendered":"\u3010LeetCode\u30110039 \u7ec4\u5408\u603b\u6570"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/combination-sum\/\">https:\/\/leetcode-cn.com\/problems\/combination-sum\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def combinationSum(self, candidates, target):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> candidates: List[int]\n        <\/em><strong><em>:type<\/em><\/strong><em> target: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: List[List[int]]\n        \"\"\"\n        <\/em>candidates.sort()\n        n = len(candidates)\n        res = []\n        def backtrack(i, tmp_sum, tmp):\n            <em>\"\"\"\n            \u56de\u6eaf\u6cd5\n            <\/em><strong><em>:param<\/em><\/strong><em> i: \u5f53\u524d\u9009\u62e9\u7684\u7d22\u5f15\n            <\/em><strong><em>:param<\/em><\/strong><em> tmp_sum: \u5f53\u524d\u5df2\u9009\u62e9\u6240\u6709\u6570\u7684\u548c\n            <\/em><strong><em>:param<\/em><\/strong><em> tmp: \u5f53\u524d\u5df2\u9009\u62e9\u6570\u7684\u5217\u8868\n            <\/em><strong><em>:return<\/em><\/strong><em>:\n            \"\"\"\n            <\/em>if tmp_sum > target or i == n:\n                return\n            if tmp_sum == target:\n                res.append(tmp)\n                return\n            for j in range(i, n):\n                if tmp_sum + candidates[j] > target:\n                    break\n                backtrack(j, tmp_sum + candidates[j], tmp + [candidates[j]])\n        backtrack(0, 0, [])\n        return res\nif __name__ == '__main__':\n    candidates = [2, 3, 6, 7]\n    target = 7\n    print(Solution().combinationSum(candidates, target))\n<\/pre>\n\n\n<p><strong>\u601d\u8def<\/strong>\uff1a\u56de\u6eaf\u6cd5\uff0c\u5341\u5206\u7c7b\u4f3c\u4e8e0037\u90a3\u4e2a\u89e3\u6570\u72ec\uff0c\u8bf4\u8d77\u6765\u5176\u5b9e\u4e5f\u662f\u7c7b\u4f3c\u4e8e\u7a77\u4e3e\u4e86\u3002<\/p>\n\n\n<p>\u5148\u4f9d\u6b21\u5bf9\u5217\u8868\u6392\u5e8f\uff0c\u4f9d\u6b21\u9009\u5b9a\u6570\u5b57\u76f4\u5230\u548c\u5927\u4e8etarget\u7136\u540e\u56de\u6eaf\uff0c\u8fd9\u6837\u4e00\u76f4\u56de\u6eaf\uff0c\u8981\u4e48\u6240\u6709\u7684\u6570\u5b57\u90fd\u88ab\u9009\u5b8c\uff0c\u8981\u4e48\u548c\u7b49\u4e8etarget\u653e\u5165res\u5217\u8868\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/combination-sum\/  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":1482,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2879"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2879"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2879\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2879"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}