{"id":355,"date":"2020-09-26T10:30:24","date_gmt":"2020-09-26T02:30:24","guid":{"rendered":"http:\/\/39.96.58.60\/?p=355"},"modified":"2022-10-18T16:39:49","modified_gmt":"2022-10-18T08:39:49","slug":"leetcode-40-%e7%bb%84%e5%90%88%e6%80%bb%e5%92%8c-ii","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=355","title":{"rendered":"Leetcode 40. \u7ec4\u5408\u603b\u548c II"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\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\u6bcf\u4e2a\u6570\u5b57\u5728\u6bcf\u4e2a\u7ec4\u5408\u4e2d\u53ea\u80fd\u4f7f\u7528\u4e00\u6b21\u3002<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u6240\u6709\u6570\u5b57\uff08\u5305\u62ec\u76ee\u6807\u6570\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:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> candidates =&nbsp;<code>[10,1,2,7,6,1,5]<\/code>, target =&nbsp;<code>8<\/code>,\n\n<strong>\u6240\u6c42\u89e3\u96c6\u4e3a:<\/strong>\n\n[\n\n  [1, 7],\n\n  [1, 2, 5],\n\n  [2, 6],\n\n  [1, 1, 6]\n\n]\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> candidates =&nbsp;[2,5,2,1,2], target =&nbsp;5,\n\n<strong>\u6240\u6c42\u89e3\u96c6\u4e3a:<\/strong>\n\n[\n\n&nbsp; [1,2,2],\n\n&nbsp; [5]\n\n]<\/pre>\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\uff1a116 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8627.35% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8669.31% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def combinationSum2(self, candidates: List[int], target: int) -&gt; List[List[int]]:\n        candidates.sort()   # \u5148\u8fdb\u884c\u6392\u5e8f\uff0c\u4e3a\u53bb\u91cd\u505a\u51c6\u5907\n        n = len(candidates)\n        result = []         # \u4fdd\u5b58\u6700\u7ec8\u7ed3\u679c\n\n        def backtrack(begin:int, current:list): # \u5f53\u524d\u6570\u5b57\u4e0b\u6807\uff0c\u5f53\u524d\u5217\u8868\n\n            if sum(current) == target:          # \u5982\u679c\u5f53\u524d\u5217\u8868\u548c == \u76ee\u6807\uff0c\u52a0\u5165\u6700\u7ec8\u7ed3\u679c\n                result.append(current.copy())\n                return\n            if sum(current) &gt; target:           # &gt; \u76ee\u6807\u503c\uff0c\u76f4\u63a5\u8fd4\u56de\n                return\n            for i in range(begin, n):\n                if i &gt; begin and candidates[i]== candidates[i-1]:   # \u6392\u5e8f\u8fc7\u4e86\uff0c\u5982\u679c\u5f53\u524d\u6570\u5b57\u5df2\u7ecf\u5904\u7406\u8fc7\uff0c\u5219\u8df3\u8fc7\n                    continue\n                current.append(candidates[i])   # \u6dfb\u52a0\u5230\u5f53\u524d\u5217\u8868\u4e2d\n                backtrack(i+1, current)         # \u67e5\u8be2\u4e0b\u4e00\u4e2a\n                current.pop()                   # \u56de\u6eaf\n\n        backtrack(0, [])\n        return result\n\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a52 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8675.99% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8690.56% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u56de\u6eaf\n    \u57fa\u672c\u601d\u60f3\u4e0e\u4e0a\u9762\u76f8\u540c\uff0c\u5f53\u65f6\u4e0d\u5bf9\u5f53\u524d\u5217\u8868\u6c42\u548c\uff0c\u800c\u662f\u5728\u6bcf\u6b21\u9012\u5f52\u65f6\uff0c\u51cf\u53bb\u5f53\u524d\u503c\uff0c\u751f\u6210\u65b0\u7684\u76ee\u6807\u503c\n    \u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u5f53\u524d\u503c\u4e0e\u76ee\u6807\u503c\uff0c\u8df3\u8fc7\u8bb8\u591a\u4e0d\u5fc5\u8981\u7684\u5faa\u73af\n\"\"\"\nclass Solution:\n    def combinationSum2(self, candidates: List[int], target: int) -&gt; List[List[int]]:\n        candidates.sort()\n        n = len(candidates)\n        result = []\n\n        def backtrack(begin:int, target:int, current:list): # \u5f53\u524d\u6570\u5b57\u4e0b\u6807\uff0c\u5f53\u524d\u76ee\u6807\u503c\uff0c\u5f53\u524d\u5217\u8868\n            if target == 0:                                 # \u76ee\u6807\u503c\u4e3a0\uff0c \u7b49\u540c\u4e8e\u5f53\u524d\u5217\u8868\u548c==\u76ee\u6807\u503c\n                result.append(current.copy())\n                return\n\n            for i in range(begin, n):\n                if candidates[i] &gt; target:                  # \u5982\u679c\u5f53\u524d\u503c\u5927\u4e8e\u5f53\u524d\u76ee\u6807\u503c\uff0c\u5219\u8df3\u8fc7\u4e4b\u540e\u7684\uff0c\u56e0\u4e3a\u5df2\u6392\u5e8f\n                    return\n\n                if i &gt; begin and candidates[i] == candidates[i-1]:\n                    continue\n                current.append(candidates[i])\n                backtrack(i+1, target-candidates[i], current)   # \u6539\u4e3a\u66f4\u65b0\u76ee\u6807\u503c\n                current.pop()\n\n        backtrack(0,target,[])\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4&nbsp;candidates&nbsp;\u548c\u4e00\u4e2a\u76ee\u6807\u6570&nbsp;target&nbsp;\uff0c\u627e\u51fa&#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":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/355"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=355"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions"}],"predecessor-version":[{"id":356,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/355\/revisions\/356"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=355"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}