{"id":395,"date":"2020-09-26T10:36:40","date_gmt":"2020-09-26T02:36:40","guid":{"rendered":"http:\/\/39.96.58.60\/?p=395"},"modified":"2022-10-18T16:39:35","modified_gmt":"2022-10-18T08:39:35","slug":"leetcode-394-%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%a7%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=395","title":{"rendered":"Leetcode 394. \u5b57\u7b26\u4e32\u89e3\u7801"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u7ecf\u8fc7\u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5b83\u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u7f16\u7801\u89c4\u5219\u4e3a: <code>k[encoded_string]<\/code>\uff0c\u8868\u793a\u5176\u4e2d\u65b9\u62ec\u53f7\u5185\u90e8\u7684 <em>encoded_string<\/em> \u6b63\u597d\u91cd\u590d <em>k<\/em> \u6b21\u3002\u6ce8\u610f <em>k<\/em> \u4fdd\u8bc1\u4e3a\u6b63\u6574\u6570\u3002<\/p>\n<p>\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u8f93\u5165\u5b57\u7b26\u4e32\u603b\u662f\u6709\u6548\u7684\uff1b\u8f93\u5165\u5b57\u7b26\u4e32\u4e2d\u6ca1\u6709\u989d\u5916\u7684\u7a7a\u683c\uff0c\u4e14\u8f93\u5165\u7684\u65b9\u62ec\u53f7\u603b\u662f\u7b26\u5408\u683c\u5f0f\u8981\u6c42\u7684\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u539f\u59cb\u6570\u636e\u4e0d\u5305\u542b\u6570\u5b57\uff0c\u6240\u6709\u7684\u6570\u5b57\u53ea\u8868\u793a\u91cd\u590d\u7684\u6b21\u6570 <em>k<\/em> \uff0c\u4f8b\u5982\u4e0d\u4f1a\u51fa\u73b0\u50cf&nbsp;<code>3a<\/code>&nbsp;\u6216&nbsp;<code>2[4]<\/code>&nbsp;\u7684\u8f93\u5165\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"3[a]2[bc]\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"aaabcbc\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"3[a2[c]]\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"accaccacc\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"2[abc]3[cd]ef\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"abcabccdcdcdef\"\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>s = \"abc3[cd]xyz\"\n\n<strong>\u8f93\u51fa\uff1a<\/strong>\"abccdcdcdxyz\"\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u6808\u3001 \u6df1\u5ea6\u4f18\u5148\u641c\u7d22\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\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8686.04% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e865.42% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u9012\u5f52\n    \u5f53\u9047\u5230\u6570\u5b57\u65f6\uff0c\u9700\u8981\u8003\u8651\u8fde\u7eed\u6570\u5b57\u7ec4\u6210\u7684\u6570\u5b57\n    \u5f53\u9047\u5230\u5b57\u6bcd\u65f6\uff0c\u76f4\u63a5\u6dfb\u52a0\u5230\u6700\u7ec8\u7ed3\u679c\u4e2d\n    \u5f53\u9047\u5230[]\u65f6\uff0c\u9012\u5f52\u5904\u7406[]\u4e2d\u7684\u5b57\u7b26,\n        \u9047\u5230[\u65f6\uff0c\u4e0b\u4e00\u4e2a\u5b57\u7b26\u5f00\u59cb\u9012\u5f52\u5904\u7406\n        \u9047\u5230]\u65f6\uff0c\u8fd4\u56de\u8fd4\u56de\u5f53\u524d\u5b57\u7b26\u4e32, \u5373[]\u4e2d\u5b57\u7b26\u4e32\n    \u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\uff0c\u56e0\u4e3a[]\u4e2d\u5b57\u7b26\u5df2\u7ecf\u5728\u9012\u5f52\u4e2d\u5904\u7406\u8fc7\u4e86\uff0c\u5728\u5916\u9762\u9700\u8981\u8df3\u8fc7\uff0c\u6240\u4ee5\u9700\u8981\u8bb0\u5f55\u5f53\u524d\u4e0b\u6807\n\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def decodeString(self, s: str) -&gt; str:\n        self.result = []\n        self.s = s\n        def find(i):    # \u4eces\u7684\u7b2ci\u4e2a\u5b57\u7b26\u5f00\u59cb\u5904\u7406, \u7528\u4e8e\u8df3\u8fc7[]\u4e2d\u5df2\u9012\u5f52\u5904\u7406\u8fc7\u7684\u5b57\u7b26\n            r = ''      # \u7ed3\u679c\n            num = 0\n            while i &lt; len(self.s):  # \u5904\u7406s\u4e2d\u6bcf\u4e2a\u5b57\u7b26\n                if self.s[i] in '0123456789':   # \u662f\u6570\u5b57\u65f6\uff0c\u9700\u8981\u7d2f\u8ba1\n                    num = num * 10 + int(self.s[i])\n                elif self.s[i] == '[':  # \u5f00\u542f\u9012\u5f52\uff0c\u5904\u7406[]\u4e2d\u5b57\u7b26\n                    rs, ri = find(i+1)  # \u8fd4\u56de[]\u4e2d\u5b57\u7b26\uff0c\u4ee5\u53ca]\u7684\u4e0b\u6807\n                    r = r + num * rs\n                    i = ri  # \u8df3\u8fc7[]\u4e2d\u5df2\u5904\u7406\u8fc7\u7684\u5b57\u7b26\n                    num = 0 # \u91cd\u7f6enum\n                elif self.s[i] == ']':  # ]\u65f6\uff0c\u9700\u8981\u8fd4\u56de\u5f53\u524d\u5b57\u7b26\u4e32\uff0c\u548c\u5f53\u524d]\u7684\u4e0b\u6807\n                    return r, i\n                else:   # \u5b57\u6bcd\u65f6\uff0c\u76f4\u63a5\u6dfb\u52a0\u5230\u5f53\u524d\u5b57\u7b26\u4e32\n                    r += self.s[i]\n                i += 1\n            return r    # \u5904\u7406\u5b8c\u6240\u6709\u5b57\u7b26\u540e, \u5373\u4e3a\u6700\u7ec8\u7ed3\u679c\n        return find(0)<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u7ecf\u8fc7\u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u5b83\u89e3\u7801\u540e\u7684\u5b57\u7b26\u4e32\u3002 \u7f16\u7801\u89c4\u5219\u4e3a: k[encoded_string]\uff0c\u8868\u793a\u5176\u4e2d\u65b9&#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\/395"}],"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=395"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":396,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/395\/revisions\/396"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}