{"id":383,"date":"2020-09-26T10:35:04","date_gmt":"2020-09-26T02:35:04","guid":{"rendered":"http:\/\/39.96.58.60\/?p=383"},"modified":"2022-10-18T16:39:35","modified_gmt":"2022-10-18T08:39:35","slug":"leetcode-38-%e5%a4%96%e8%a7%82%e6%95%b0%e5%88%97","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=383","title":{"rendered":"Leetcode 38. \u5916\u89c2\u6570\u5217"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 <em>n<\/em>\uff081 \u2264&nbsp;<em>n<\/em>&nbsp;\u2264 30\uff09\uff0c\u8f93\u51fa\u5916\u89c2\u6570\u5217\u7684\u7b2c <em>n<\/em> \u9879\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u6574\u6570\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u9879\u5c06\u8868\u793a\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u300c\u5916\u89c2\u6570\u5217\u300d\u662f\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\uff0c\u4ece\u6570\u5b57 1 \u5f00\u59cb\uff0c\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e00\u9879\u90fd\u662f\u5bf9\u524d\u4e00\u9879\u7684\u63cf\u8ff0\u3002\u524d\u4e94\u9879\u5982\u4e0b\uff1a<\/p>\n<pre>1.     1\n\n2.     11\n\n3.     21\n\n4.     1211\n\n5.     111221\n\n<\/pre>\n<p>\u7b2c\u4e00\u9879\u662f\u6570\u5b57 1<\/p>\n<p>\u63cf\u8ff0\u524d\u4e00\u9879\uff0c\u8fd9\u4e2a\u6570\u662f <code>1<\/code> \u5373 \u201c\u4e00\u4e2a 1 \u201d\uff0c\u8bb0\u4f5c <code>11<\/code><\/p>\n<p>\u63cf\u8ff0\u524d\u4e00\u9879\uff0c\u8fd9\u4e2a\u6570\u662f <code>11<\/code> \u5373 \u201c\u4e24\u4e2a 1 \u201d \uff0c\u8bb0\u4f5c <code>21<\/code><\/p>\n<p>\u63cf\u8ff0\u524d\u4e00\u9879\uff0c\u8fd9\u4e2a\u6570\u662f <code>21<\/code> \u5373 \u201c\u4e00\u4e2a 2 \u4e00\u4e2a 1 \u201d \uff0c\u8bb0\u4f5c <code>1211<\/code><\/p>\n<p>\u63cf\u8ff0\u524d\u4e00\u9879\uff0c\u8fd9\u4e2a\u6570\u662f <code>1211<\/code> \u5373 \u201c\u4e00\u4e2a 1 \u4e00\u4e2a 2 \u4e24\u4e2a 1 \u201d \uff0c\u8bb0\u4f5c <code>111221<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b&nbsp;1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 1\n\n<strong>\u8f93\u51fa:<\/strong> \"1\"\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u8fd9\u662f\u4e00\u4e2a\u57fa\u672c\u6837\u4f8b\u3002<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> 4\n\n<strong>\u8f93\u51fa:<\/strong> \"1211\"\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5f53 n = 3 \u65f6\uff0c\u5e8f\u5217\u662f \"21\"\uff0c\u5176\u4e2d\u6211\u4eec\u6709 \"2\" \u548c \"1\" \u4e24\u7ec4\uff0c\"2\" \u53ef\u4ee5\u8bfb\u4f5c \"12\"\uff0c\u4e5f\u5c31\u662f\u51fa\u73b0\u9891\u6b21 = 1 \u800c \u503c = 2\uff1b\u7c7b\u4f3c \"1\" \u53ef\u4ee5\u8bfb\u4f5c \"11\"\u3002\u6240\u4ee5\u7b54\u6848\u662f \"12\" \u548c \"11\" \u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u4e5f\u5c31\u662f \"1211\"\u3002<\/pre>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u5b57\u7b26\u4e32\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\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8692.56% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.07% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u521d\u59cb\u5316 n\u4e3a1\u65f6\uff0creturn '1'\n    \u5f53n&gt;1\u65f6\uff0c\u5f00\u59cb\u5faa\u73af\uff0c\u8ba1\u7b97\u5916\u89c2\u6570\u5217\u7684\u7b2c n \u9879\n    \u8be6\u7ec6\u903b\u8f91\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\n\nclass Solution:\n    def countAndSay(self, n: int) -&gt; str:\n        string = '1'        # \u521d\u59cb\u7684\u5b57\u7b26\u4e32\u4e3a'1'\uff0c n\u4e3a1\u65f6\uff0c\u5916\u89c2\u6570\u5217\u4e5f\u4e3a'1'\n        for _ in range(1, n):   # \u5f53n&gt;1\u65f6\uff0c\u4f9d\u6b21\u5faa\u73af\u8ba1\u7b97\u5916\u89c2\u6570\u5217\n            s = ''              # s\u7528\u4e8e\u8bb0\u5f55\u5916\u89c2\u6570\u5217\n            record_s = string[0]    # n-1\u5916\u89c2\u6570\u5217\u7684\u7b2c\u4e00\u9879\uff0c \u5f85\u6bd4\u8f83\u9879\n            record_n = 1            # \u7528\u4e8e\u8ba1\u6570\n            for i in range(1, len(string)): # \u4ece\u7b2c\u4e8c\u9879\u5f00\u59cb\u6bd4\u8f83\uff0c\u5b57\u7b26\u662f\u5426\u76f8\u540c\n                if string[i] == record_s:   # \u5982\u679c\u5b57\u7b26\u76f8\u540c\n                    record_n += 1           # \u8ba1\u6570+1\n                else:\n                    s = s + str(record_n) + record_s    # \u4e0d\u76f8\u540c\u65f6\uff0c\u5199\u5165s\n                    record_s = string[i]                # \u5c06\u5f85\u6bd4\u8f83\u9879 \u66f4\u65b0\u4e3a\u5f53\u524d\u4e0d\u91cd\u590d\u9879\n                    record_n = 1                        # \u5c06\u8ba1\u6570 \u7f6e\u4e3a1\n            s = s + str(record_n) + record_s            # \u5916\u89c2\u6570\u5217\u5faa\u73af\u5b8c\uff0c\u5904\u7406\u6700\u540e\u4e00\u9879(\u6216n\u9879)\n            string = s                                  # \u5c06\u5916\u89c2\u6570\u5217\u66f4\u65b0\n        return string<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 n\uff081 \u2264&nbsp;n&nbsp;\u2264 30\uff09\uff0c\u8f93\u51fa\u5916\u89c2\u6570\u5217\u7684\u7b2c n \u9879\u3002 \u6ce8\u610f\uff1a\u6574\u6570\u5e8f\u5217\u4e2d\u7684&#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\/383"}],"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=383"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions"}],"predecessor-version":[{"id":384,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/383\/revisions\/384"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=383"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}