{"id":353,"date":"2020-09-26T10:30:09","date_gmt":"2020-09-26T02:30:09","guid":{"rendered":"http:\/\/39.96.58.60\/?p=353"},"modified":"2022-10-18T16:39:49","modified_gmt":"2022-10-18T08:39:49","slug":"leetcode-39-%e7%bb%84%e5%90%88%e6%80%bb%e5%92%8c","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=353","title":{"rendered":"Leetcode 39. \u7ec4\u5408\u603b\u548c"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a<strong>\u65e0\u91cd\u590d\u5143\u7d20<\/strong>\u7684\u6570\u7ec4&nbsp;<code>candidates<\/code>&nbsp;\u548c\u4e00\u4e2a\u76ee\u6807\u6570&nbsp;<code>target<\/code>&nbsp;\uff0c\u627e\u51fa&nbsp;<code>candidates<\/code>&nbsp;\u4e2d\u6240\u6709\u53ef\u4ee5\u4f7f\u6570\u5b57\u548c\u4e3a&nbsp;<code>target<\/code>&nbsp;\u7684\u7ec4\u5408\u3002<\/p>\n<p><code>candidates<\/code>&nbsp;\u4e2d\u7684\u6570\u5b57\u53ef\u4ee5\u65e0\u9650\u5236\u91cd\u590d\u88ab\u9009\u53d6\u3002<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u6240\u6709\u6570\u5b57\uff08\u5305\u62ec&nbsp;<code>target<\/code>\uff09\u90fd\u662f\u6b63\u6574\u6570\u3002<\/li>\n<li>\u89e3\u96c6\u4e0d\u80fd\u5305\u542b\u91cd\u590d\u7684\u7ec4\u5408\u3002&nbsp;<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b&nbsp;1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>candidates = <code>[2,3,6,7], <\/code>target = <code>7<\/code>,\n\n<strong>\u6240\u6c42\u89e3\u96c6\u4e3a\uff1a<\/strong>\n\n[\n\n  [7],\n\n  [2,2,3]\n\n]\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>candidates = [2,3,5]<code>, <\/code>target = 8,\n\n<strong>\u6240\u6c42\u89e3\u96c6\u4e3a\uff1a<\/strong>\n\n[\n\n&nbsp; [2,2,2,2],\n\n&nbsp; [2,3,3],\n\n&nbsp; [3,5]\n\n]<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= candidates.length &lt;= 30<\/code><\/li>\n<li><code>1 &lt;= candidates[i] &lt;= 200<\/code><\/li>\n<li><code>candidate<\/code> \u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002<\/li>\n<li><code>1 &lt;= target &lt;= 500<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6570\u7ec4\u3001 \u56de\u6eaf\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a52 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8691.06% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8642.14% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u9012\u5f52\n\"\"\"\nclass Solution:\n    def combinationSum(self, candidates: List[int], target: int) -&gt; List[List[int]]:\n        candidates.sort()\n        result = []\n        n = len(candidates)\n        def backtrack(index, target, current):  # \u9012\u5f52\uff0c\u5f00\u59cb\u7684\u4e0b\u6807\uff0c\u76ee\u6807\u503c\uff0c\u5f53\u524d\u6570\u7ec4\n            for i in range(index, n):\n                num = candidates[i]\n                if num == target:\n                    result.append(current+[num])    # \u5f53\u524d\u6570\u7ec4\u4e0b \u67d0\u503c\u4e0e\u76ee\u6807\u503c\u76f8\u7b49\uff0c\u5219\u52a0\u5165\u5230\u7ed3\u679c\u4e2d\n                elif num &gt; target:  # \u6570\u503c\u5927\u4e8e\u76ee\u6807\u503c\uff0c\u5219\u8df3\u51fa\n                    return\n                else:               # \u5c0f\u4e8e\u76ee\u6807\u503c\uff0c\u4e0b\u4e00\u5c42\n                    backtrack(i, target-num, current+[num])\n        backtrack(0, target, [])\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u65e0\u91cd\u590d\u5143\u7d20\u7684\u6570\u7ec4&nbsp;candidates&nbsp;\u548c\u4e00\u4e2a\u76ee\u6807\u6570&nbsp;target&#038;nb&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/353"}],"collection":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=353"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/353\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}