{"id":397,"date":"2020-09-26T10:36:52","date_gmt":"2020-09-26T02:36:52","guid":{"rendered":"http:\/\/39.96.58.60\/?p=397"},"modified":"2022-10-18T16:39:35","modified_gmt":"2022-10-18T08:39:35","slug":"leetcode-91-%e8%a7%a3%e7%a0%81%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.yatenglg.cn\/blog\/?p=397","title":{"rendered":"Leetcode 91. \u89e3\u7801\u65b9\u6cd5"},"content":{"rendered":"<p>\u4e00\u6761\u5305\u542b\u5b57\u6bcd&nbsp;<code>A-Z<\/code> \u7684\u6d88\u606f\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u4e86\u7f16\u7801\uff1a<\/p>\n<pre>'A' -&gt; 1\n\n'B' -&gt; 2\n\n...\n\n'Z' -&gt; 26\n\n<\/pre>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u53ea\u5305\u542b\u6570\u5b57\u7684<strong>\u975e\u7a7a<\/strong>\u5b57\u7b26\u4e32\uff0c\u8bf7\u8ba1\u7b97\u89e3\u7801\u65b9\u6cd5\u7684\u603b\u6570\u3002<\/p>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"12\"\n\n<strong>\u8f93\u51fa:<\/strong> 2\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;\u5b83\u53ef\u4ee5\u89e3\u7801\u4e3a \"AB\"\uff081 2\uff09\u6216\u8005 \"L\"\uff0812\uff09\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b&nbsp;2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> \"226\"\n\n<strong>\u8f93\u51fa:<\/strong> 3\n\n<strong>\u89e3\u91ca:<\/strong>&nbsp;\u5b83\u53ef\u4ee5\u89e3\u7801\u4e3a \"BZ\" (2 26), \"VF\" (22 6), \u6216\u8005 \"BBF\" (2 2 6) \u3002\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u5b57\u7b26\u4e32\u3001 \u52a8\u6001\u89c4\u5212\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\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8694.07% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8685.98% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u672c\u9898\u5206\u4ee5\u4e0b\u51e0\u79cd\u60c5\u51b5\uff1a\n        1. \"\"                       # 0\n        2. \"_\" 1 ~ 9                # 1\n        3. 'x10'                    # dp[i-2]\n        4. 'x1_' x11 ~ x19          # dp[i-1] + dp[i-2]\n        5. 'x20'                    # dp[i-2]\n        6. 'x2_' x21 ~ x26          # dp[i-1] + dp[i-2]\n        7. 'x2_' x27 ~ x29          # dp[i-1]\n        8. 'x3_' x31 ~ x39          # dp[i-1]\n        9. 'x30'                    # 0\n       11. 'x00'                    # 0\n       10. 'x0_' x01 ~ x09          # dp[i-1]\n    \u5206\u522b\u5904\u7406\u5373\u53ef\n\"\"\"\nclass Solution:\n    def numDecodings(self, s: str) -&gt; int:\n        n = len(s)\n\n        if n == 0 or s[0] == '0':\n            return 0\n\n        dp = [[] for _ in range(n+1)]\n        dp[0] = 1\n        dp[1] = 1\n        if n == 1:\n            return dp[1]\n\n        for i in range(1, n):\n            if s[i-1] == '0':   # 0_\n                if s[i] == '0':     # x00\n                    return 0\n                else:               # x01 x02 x03 ~ x09\n                    dp[i+1] = dp[i+1-1]\n            elif s[i-1] == '1':     # x1_\n                if s[i] == '0':     # x10\n                    dp[i+1] = dp[i+1-2]\n                else:               # x11 ~ x19\n                    dp[i+1] = dp[i+1-1] + dp[i+1-2]\n            elif s[i-1] == '2':     # x2_\n                if s[i] == '0':  # x20\n                    dp[i+1] = dp[i+1-2]\n                elif s[i] in '789': # x27 ~ x29\n                    dp[i+1] = dp[i+1-1]\n                else:               # x21 ~ x26\n                    dp[i+1] = dp[i+1-1] + dp[i+1-2]\n            else:                   # x3_ ~ x9~\n                if s[i] == '0':\n                    return 0\n                else:\n                    dp[i+1] = dp[i+1-1]\n        return dp[-1]<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u6761\u5305\u542b\u5b57\u6bcd&nbsp;A-Z \u7684\u6d88\u606f\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u4e86\u7f16\u7801\uff1a &#8216;A&#8217; -&gt; 1 &#8216;B&#8217; -&gt; 2&#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\/397"}],"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=397"}],"version-history":[{"count":1,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}