{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "cc5dccee574d40d1b46dba4b524fdca4",
  "keywords": "字符串",
  "title": "外观数列",
  "desc": [
    {
      "content": "\n<div class=\"notranslate\">\n<p>给定一个正整数 <code>n</code> ，输出外观数列的第 <code>n</code> 项。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>「外观数列」是一个整数序列，从数字 1 开始，序列中的每一项都是对前一项的描述。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>你可以将其视作是由递归公式定义的数字字符串序列：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>countAndSay(1) = \"1\"</code></li>\n<li><code>countAndSay(n)</code> 是对 <code>countAndSay(n-1)</code> 的描述，然后转换成另一个数字字符串。</li>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>前五项如下：</p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre>\n1.     1\n2.     11\n3.     21\n4.     1211\n5.     111221\n第一项是数字 1 \n描述前一项，这个数是 1 即 “ 一 个 1 ”，记作 \"11\"\n描述前一项，这个数是 11 即 “ 二 个 1 ” ，记作 \"21\"\n描述前一项，这个数是 21 即 “ 一 个 2 + 一 个 1 ” ，记作 \"1211\"\n描述前一项，这个数是 1211 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ，记作 \"111221\"\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>要 <strong>描述</strong> 一个数字字符串，首先要将字符串分割为 <strong>最小</strong> 数量的组，每个组都由连续的最多 <strong>相同字符</strong>\n组成。然后对于每个组，先描述字符的数量，然后描述字符，形成一个描述组。要将描述转换为数字字符串，先将每组中的字符数量用数字替换，再将所有描述组连接起来。</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p>例如，数字字符串 <code>\"3322251\"</code> 的描述如下图：</p>\n<img alt=\"\"\nsrc=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0038.Count%20and%20Say/images/countandsay.jpg\"\nstyle=\"width: 581px; height: 172px;\" />\n<ul>\n</ul>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 1：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>n = 1\n<strong>输出：</strong>\"1\"\n<strong>解释：</strong>这是一个基本样例。\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>示例 2：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<pre><strong>输入：</strong>n = 4\n<strong>输出：</strong>\"1211\"\n<strong>解释：</strong>\ncountAndSay(1) = \"1\"\ncountAndSay(2) = 读 \"1\" = 一 个 1 = \"11\"\ncountAndSay(3) = 读 \"11\" = 二 个 1 = \"21\"\ncountAndSay(4) = 读 \"21\" = 一 个 2 + 一 个 1 = \"12\" + \"11\" = \"1211\"\n</pre>",
      "language": "markdown"
    },
    {
      "content": "\n<p>&nbsp;</p>",
      "language": "markdown"
    },
    {
      "content": "\n<p><strong>提示：</strong></p>",
      "language": "markdown"
    },
    {
      "content": "\n<ul>\n<li><code>1 &lt;= n &lt;= 30</code></li>\n</ul>\n</div>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class Solution:\n\tdef countAndSay(self, n):\n\t\tif n == 1:\n\t\t\treturn '1'\n\t\tx = '1'\n\t\twhile n > 1:\n\t\t\tx = self.count(x)\n\t\t\tn -= 1\n\t\treturn x\n\tdef count(self, x):\n\t\tm = list(x)\n\t\tres = []\n\t\tm.append(None)\n\t\ti , j = 0 , 0\n\t\twhile i < len(m) - 1:\n\t\t\tj += 1\n\t\t\tif m[j] != m[i]:\n\t\t\t\tres += [j - i, m[i]]\n\t\t\t\ti = j\n\t\treturn ''.join(str(s) for s in res)\n# %%\ns = Solution()\nprint(s.countAndSay(n = 4))",
    "language": "python"
  },
  "node_id": "dailycode-89a4d210cf6b4a96ab49088e293fdd9f",
  "license": "csdn.net",
  "created_at": 1637894159,
  "topic_link": "https://bbs.csdn.net/topics/600470795"
}