{"id":357,"date":"2020-09-26T10:30:39","date_gmt":"2020-09-26T02:30:39","guid":{"rendered":"http:\/\/39.96.58.60\/?p=357"},"modified":"2022-10-18T16:39:49","modified_gmt":"2022-10-18T08:39:49","slug":"leetcode-216-%e7%bb%84%e5%90%88%e6%80%bb%e5%92%8c-iii","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=357","title":{"rendered":"Leetcode 216. \u7ec4\u5408\u603b\u548c III"},"content":{"rendered":"<p>\u627e\u51fa\u6240\u6709\u76f8\u52a0\u4e4b\u548c\u4e3a&nbsp;<em><strong>n<\/strong> <\/em>\u7684&nbsp;<strong><em>k&nbsp;<\/em><\/strong>\u4e2a\u6570\u7684\u7ec4\u5408<strong><em>\u3002<\/em><\/strong>\u7ec4\u5408\u4e2d\u53ea\u5141\u8bb8\u542b\u6709 1 &#8211;&nbsp;9 \u7684\u6b63\u6574\u6570\uff0c\u5e76\u4e14\u6bcf\u79cd\u7ec4\u5408\u4e2d\u4e0d\u5b58\u5728\u91cd\u590d\u7684\u6570\u5b57\u3002<\/p>\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n<ul>\n<li>\u6240\u6709\u6570\u5b57\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 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> <em><strong>k<\/strong><\/em> = 3, <em><strong>n<\/strong><\/em> = 7\n\n<strong>\u8f93\u51fa:<\/strong> [[1,2,4]]\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> <em><strong>k<\/strong><\/em> = 3, <em><strong>n<\/strong><\/em> = 9\n\n<strong>\u8f93\u51fa:<\/strong> [[1,2,6], [1,3,5], [2,3,4]]\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\uff1a28 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8699.19% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.8 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.93% \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 combinationSum3(self, k: int, n: int) -&gt; List[List[int]]:\n        nums = list(range(1, 10))   # 1~9\n        result = []\n\n        def backtrack(begin, current:list):\n\n            if sum(current) == n and len(current) == k: # \u5982\u679c\u548c\u7b49\u4e8en, \u4e14\u4e2a\u6570\u7b49\u4e8ek\uff0c \u5219\u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n                result.append(current[:])\n                return\n\n            if sum(current) &gt; n or len(current) &gt; k:    # \u5982\u679c\u548c\u5927\u4e8e9 \u6216 \u5f53\u524d\u5217\u8868\u957f\u5ea6\u5927\u4e8ek\uff0c \u5219\u8df3\u51fa\n                return\n\n            for i in range(begin, 9):\n                current.append(nums[i])\n                backtrack(i+1, current) # \u67e5\u770b\u4e0b\u4e00\u4e2a\n                current.pop()   # \u56de\u6eaf\n\n        backtrack(0, [])\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u627e\u51fa\u6240\u6709\u76f8\u52a0\u4e4b\u548c\u4e3a&nbsp;n \u7684&nbsp;k&nbsp;\u4e2a\u6570\u7684\u7ec4\u5408\u3002\u7ec4\u5408\u4e2d\u53ea\u5141\u8bb8\u542b\u6709 1 &#8211;&#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\/357"}],"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=357"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions"}],"predecessor-version":[{"id":358,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/357\/revisions\/358"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=357"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}