1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-05 15:31:43 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/外观数列 [count-and-say].html
2025-01-09 20:29:41 +08:00

56 lines
2.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>「外观数列」是一个数位字符串序列,由递归公式定义:</p>
<ul>
<li><code>countAndSay(1) = "1"</code></li>
<li><code>countAndSay(n)</code>&nbsp;<code>countAndSay(n-1)</code> 的行程长度编码。</li>
</ul>
<p>&nbsp;</p>
<ul>
</ul>
<p><a href="https://baike.baidu.com/item/%E8%A1%8C%E7%A8%8B%E9%95%BF%E5%BA%A6%E7%BC%96%E7%A0%81/2931940">行程长度编码</a>RLE是一种字符串压缩方法其工作原理是通过将连续相同字符重复两次或更多次替换为字符重复次数运行长度和字符的串联。例如要压缩字符串&nbsp;<code>"3322251"</code>&nbsp;,我们将&nbsp;<code>"33"</code>&nbsp;&nbsp;<code>"23"</code>&nbsp;替换,将&nbsp;<code>"222"</code>&nbsp;&nbsp;<code>"32"</code>&nbsp;替换,将&nbsp;<code>"5"</code>&nbsp;&nbsp;<code>"15"</code>&nbsp;替换并将&nbsp;<code>"1"</code>&nbsp;&nbsp;<code>"11"</code>&nbsp;替换。因此压缩后字符串变为 <code>"23321511"</code></p>
<p>给定一个整数&nbsp;<code>n</code>&nbsp;,返回&nbsp;<strong>外观数列</strong>&nbsp;的第&nbsp;<code>n</code>&nbsp;个元素。</p>
<p><strong>示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong>n = 4</p>
<p><strong>输出:</strong>"1211"</p>
<p><strong>解释:</strong></p>
<p>countAndSay(1) = "1"</p>
<p>countAndSay(2) = "1" 的行程长度编码 = "11"</p>
<p>countAndSay(3) = "11" 的行程长度编码 = "21"</p>
<p>countAndSay(4) = "21" 的行程长度编码 = "1211"</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">n = 1</span></p>
<p><strong>输出:</strong><span class="example-io">"1"</span></p>
<p><strong>解释:</strong></p>
<p>这是基本情况。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 30</code></li>
</ul>
<p>&nbsp;</p>
<strong>进阶:</strong>你能迭代解决该问题吗?