{"id":304,"date":"2020-09-26T10:22:24","date_gmt":"2020-09-26T02:22:24","guid":{"rendered":"http:\/\/39.96.58.60\/?p=304"},"modified":"2022-10-18T16:43:41","modified_gmt":"2022-10-18T08:43:41","slug":"leetcode-306-%e7%b4%af%e5%8a%a0%e6%95%b0","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=304","title":{"rendered":"Leetcode 306. \u7d2f\u52a0\u6570"},"content":{"rendered":"<p>\u7d2f\u52a0\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7ec4\u6210\u5b83\u7684\u6570\u5b57\u53ef\u4ee5\u5f62\u6210\u7d2f\u52a0\u5e8f\u5217\u3002<\/p>\n<p>\u4e00\u4e2a\u6709\u6548\u7684\u7d2f\u52a0\u5e8f\u5217\u5fc5\u987b<strong>\u81f3\u5c11<\/strong>\u5305\u542b 3 \u4e2a\u6570\u3002\u9664\u4e86\u6700\u5f00\u59cb\u7684\u4e24\u4e2a\u6570\u4ee5\u5916\uff0c\u5b57\u7b26\u4e32\u4e2d\u7684\u5176\u4ed6\u6570\u90fd\u7b49\u4e8e\u5b83\u4e4b\u524d\u4e24\u4e2a\u6570\u76f8\u52a0\u7684\u548c\u3002<\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b\u6570\u5b57&nbsp;<code>'0'-'9'<\/code>&nbsp;\u7684\u5b57\u7b26\u4e32\uff0c\u7f16\u5199\u4e00\u4e2a\u7b97\u6cd5\u6765\u5224\u65ad\u7ed9\u5b9a\u8f93\u5165\u662f\u5426\u662f\u7d2f\u52a0\u6570\u3002<\/p>\n<p><strong>\u8bf4\u660e:&nbsp;<\/strong>\u7d2f\u52a0\u5e8f\u5217\u91cc\u7684\u6570\u4e0d\u4f1a\u4ee5 0 \u5f00\u5934\uff0c\u6240\u4ee5\u4e0d\u4f1a\u51fa\u73b0&nbsp;<code>1, 2, 03<\/code> \u6216\u8005&nbsp;<code>1, 02, 3<\/code>&nbsp;\u7684\u60c5\u51b5\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> <code>\"112358\"<\/code>\n\n<strong>\u8f93\u51fa:<\/strong> true \n\n<strong>\u89e3\u91ca: <\/strong>\u7d2f\u52a0\u5e8f\u5217\u4e3a: <code>1, 1, 2, 3, 5, 8 <\/code>\u30021 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, 3 + 5 = 8\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> <code>\"199100199\"<\/code>\n\n<strong>\u8f93\u51fa:<\/strong> true \n\n<strong>\u89e3\u91ca: <\/strong>\u7d2f\u52a0\u5e8f\u5217\u4e3a: <code>1, 99, 100, 199\u3002<\/code>1 + 99 = 100, 99 + 100 = 199<\/pre>\n<p><strong>\u8fdb\u9636:<\/strong><\/p>\n<p>\u4f60\u5982\u4f55\u5904\u7406\u4e00\u4e2a\u6ea2\u51fa\u7684\u8fc7\u5927\u7684\u6574\u6570\u8f93\u5165?<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \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\uff1a44 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8665.97% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8654.62% \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 isAdditiveNumber(self, num: str) -&gt; bool:\n        n = len(num)\n        def backtrack(num, current):\n            # print(num, current)\n            if num == '' and len(current) &gt; 2:  # \u5982\u679c\u5f53\u524d\u5212\u5206\u5927\u4e8e2\uff0c\u4e14\u5b57\u7b26\u4e32\u5747\u5904\u7406\u5b8c\u6210\uff0c\u5219\u8fd4\u56deTrue\n                return True\n            if num == '':   # \u5982\u679c\u5b57\u7b26\u4e32\u5747\u5904\u7406\u5b8c\uff0c\u5219\u8df3\u51fa\n                return\n            for i in range(1, n):\n                if (num[0] == '0' and i &lt; 2) or num[0]!='0':    # \u9488\u5bf9\u4ee50\u5f00\u5934\u7684\u6570\u5b57\u5904\u7406\uff0c\u598202 03\n                    if len(current)&lt;2 or (len(current) &gt;= 2 and int(num[:i])==int(current[-1])+int(current[-2])):   # \u5982\u5f53\u524d\u6570\u5b57\u4e2a\u6570\u5c0f\u4e8e2\uff0c\u5219\u76f4\u63a5\u6dfb\u52a0\u3002\u5982\u5927\u4e8e2\uff0c\u5219\u9700\u5224\u522b\u662f\u5426\u548c\u7b49\u4e8e\u524d\u4e24\u4e2a\u6570\u60f3\u52a0\n                        current.append(num[:i])\n                        new_num = num[i:]\n                        if backtrack(new_num, current): # \u5728\u5269\u4f59\u5b57\u7b26\u4e32\u4e2d\u5bfb\u627e\u4e0b\u4e00\u4e2a\u6570\u5b57\n                            return True\n                        current.pop()   # \u56de\u6eaf\n\n        return backtrack(num, [])\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7d2f\u52a0\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7ec4\u6210\u5b83\u7684\u6570\u5b57\u53ef\u4ee5\u5f62\u6210\u7d2f\u52a0\u5e8f\u5217\u3002 \u4e00\u4e2a\u6709\u6548\u7684\u7d2f\u52a0\u5e8f\u5217\u5fc5\u987b\u81f3\u5c11\u5305\u542b 3 \u4e2a\u6570\u3002\u9664\u4e86\u6700\u5f00\u59cb\u7684\u4e24\u4e2a&#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\/304"}],"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=304"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":305,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions\/305"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}