{"id":2120,"date":"2022-01-18T16:51:05","date_gmt":"2022-01-18T08:51:05","guid":{"rendered":"http:\/\/139.9.1.231\/?p=2120"},"modified":"2022-01-18T16:51:07","modified_gmt":"2022-01-18T08:51:07","slug":"leetcodeday38","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/01\/18\/leetcodeday38\/","title":{"rendered":"leetcodeday38&#8211;\u5916\u89c2\u6570\u5217"},"content":{"rendered":"\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570&nbsp;<code>n<\/code>&nbsp;\uff0c\u8f93\u51fa\u5916\u89c2\u6570\u5217\u7684\u7b2c&nbsp;<code>n<\/code>&nbsp;\u9879\u3002<\/p>\n\n\n\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<\/p>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u5c06\u5176\u89c6\u4f5c\u662f\u7531\u9012\u5f52\u516c\u5f0f\u5b9a\u4e49\u7684\u6570\u5b57\u5b57\u7b26\u4e32\u5e8f\u5217\uff1a<\/p>\n\n\n\n<ul><li><code>countAndSay(1) = \"1\"<\/code><\/li><li><code>countAndSay(n)<\/code>&nbsp;\u662f\u5bf9&nbsp;<code>countAndSay(n-1)<\/code>&nbsp;\u7684\u63cf\u8ff0\uff0c\u7136\u540e\u8f6c\u6362\u6210\u53e6\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32\u3002<\/li><\/ul>\n\n\n\n<p>\u524d\u4e94\u9879\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">1.     1\n2.     11\n3.     21\n4.     1211\n5.     111221\n\u7b2c\u4e00\u9879\u662f\u6570\u5b57 1 \n\u63cf\u8ff0\u524d\u4e00\u9879\uff0c\u8fd9\u4e2a\u6570\u662f <code>1<\/code> \u5373 \u201c \u4e00 \u4e2a 1 \u201d\uff0c\u8bb0\u4f5c <code>\"11\"\n<\/code>\u63cf\u8ff0\u524d\u4e00\u9879\uff0c\u8fd9\u4e2a\u6570\u662f <code>11<\/code> \u5373 \u201c \u4e8c \u4e2a 1 \u201d \uff0c\u8bb0\u4f5c <code>\"21\"\n<\/code>\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\"\n<\/code>\u63cf\u8ff0\u524d\u4e00\u9879\uff0c\u8fd9\u4e2a\u6570\u662f <code>1211<\/code> \u5373 \u201c \u4e00 \u4e2a 1 + \u4e00 \u4e2a 2 + \u4e8c \u4e2a 1 \u201d \uff0c\u8bb0\u4f5c \"<code>111221\"<\/code>\n<\/pre>\n\n\n\n<p>\u8981&nbsp;<strong>\u63cf\u8ff0<\/strong>&nbsp;\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u9996\u5148\u8981\u5c06\u5b57\u7b26\u4e32\u5206\u5272\u4e3a&nbsp;<strong>\u6700\u5c0f<\/strong>&nbsp;\u6570\u91cf\u7684\u7ec4\uff0c\u6bcf\u4e2a\u7ec4\u90fd\u7531\u8fde\u7eed\u7684\u6700\u591a&nbsp;<strong>\u76f8\u540c\u5b57\u7b26<\/strong>&nbsp;\u7ec4\u6210\u3002\u7136\u540e\u5bf9\u4e8e\u6bcf\u4e2a\u7ec4\uff0c\u5148\u63cf\u8ff0\u5b57\u7b26\u7684\u6570\u91cf\uff0c\u7136\u540e\u63cf\u8ff0\u5b57\u7b26\uff0c\u5f62\u6210\u4e00\u4e2a\u63cf\u8ff0\u7ec4\u3002\u8981\u5c06\u63cf\u8ff0\u8f6c\u6362\u4e3a\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u5148\u5c06\u6bcf\u7ec4\u4e2d\u7684\u5b57\u7b26\u6570\u91cf\u7528\u6570\u5b57\u66ff\u6362\uff0c\u518d\u5c06\u6240\u6709\u63cf\u8ff0\u7ec4\u8fde\u63a5\u8d77\u6765\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u6570\u5b57\u5b57\u7b26\u4e32&nbsp;<code>\"3322251\"<\/code>&nbsp;\u7684\u63cf\u8ff0\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/pic.leetcode-cn.com\/1629874763-TGmKUh-image.png\" alt=\"\"\/><\/figure>\n\n\n\n<ul><li><\/li><\/ul>\n\n\n\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>n = 1\n<strong>\u8f93\u51fa\uff1a<\/strong>\"1\"\n<strong>\u89e3\u91ca\uff1a<\/strong>\u8fd9\u662f\u4e00\u4e2a\u57fa\u672c\u6837\u4f8b\u3002\n<\/pre>\n\n\n\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>\u8f93\u5165\uff1a<\/strong>n = 4\n<strong>\u8f93\u51fa\uff1a<\/strong>\"1211\"\n<strong>\u89e3\u91ca\uff1a<\/strong>\ncountAndSay(1) = \"1\"\ncountAndSay(2) = \u8bfb \"1\" = \u4e00 \u4e2a 1 = \"11\"\ncountAndSay(3) = \u8bfb \"11\" = \u4e8c \u4e2a 1 = \"21\"\ncountAndSay(4) = \u8bfb \"21\" = \u4e00 \u4e2a 2 + \u4e00 \u4e2a 1 = \"12\" + \"11\" = \"1211\"\n<\/pre>\n\n\n\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n\n\n\n<ul><li><code>1 &lt;= n &lt;= 30<\/code><\/li><\/ul>\n\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># @lc app=leetcode.cn id=38 lang=python3\r\n#\r\n# &#91;38] \u5916\u89c2\u6570\u5217\r\n#\r\n\r\n# @lc code=start\r\nfrom unittest import result\r\n\r\n\r\nclass Solution:\r\n    def countAndSay(self, n: int) -> str:\r\n      def subAndSay(n):\r\n        if n==1 :\r\n            return \"1*\"\r\n        string = subAndSay(n-1)\r\n        m=1\r\n        result=\"\"\r\n        for i in range(len(string)-1):\r\n            if string&#91;i]==string&#91;i+1]:\r\n                m=m+1\r\n            else: \r\n                result+=str(m)+string&#91;i]\r\n                m=1\r\n        result+=\"*\"\r\n        return result\r\n      return subAndSay(n)&#91;:-1]\r\n\r\n        <\/code><\/pre>\n\n\n\n<p>\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" width=\"887\" height=\"166\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-237.png\" alt=\"\" class=\"wp-image-2122\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-237.png 887w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-237-300x56.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/01\/image-237-768x144.png 768w\" sizes=\"(max-width: 887px) 100vw, 887px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570&nbsp;n&nbsp;\uff0c\u8f93\u51fa\u5916\u89c2\u6570\u5217\u7684\u7b2c&nbsp;n&nbsp;\u9879\u3002 \u300c\u5916\u89c2\u6570\u5217\u300d\u662f\u4e00\u4e2a\u6574 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/01\/18\/leetcodeday38\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">leetcodeday38&#8211;\u5916\u89c2\u6570\u5217<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2120"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=2120"}],"version-history":[{"count":6,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2120\/revisions"}],"predecessor-version":[{"id":2127,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/2120\/revisions\/2127"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=2120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=2120"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=2120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}