mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			50 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>The <strong>count-and-say</strong> sequence is a sequence of digit strings defined by the recursive formula:</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>countAndSay(1) = "1"</code></li>
 | 
						|
	<li><code>countAndSay(n)</code> is the run-length encoding of <code>countAndSay(n - 1)</code>.</li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p><a href="http://en.wikipedia.org/wiki/Run-length_encoding" target="_blank">Run-length encoding</a> (RLE) is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string <code>"3322251"</code> we replace <code>"33"</code> with <code>"23"</code>, replace <code>"222"</code> with <code>"32"</code>, replace <code>"5"</code> with <code>"15"</code> and replace <code>"1"</code> with <code>"11"</code>. Thus the compressed string becomes <code>"23321511"</code>.</p>
 | 
						|
 | 
						|
<p>Given a positive integer <code>n</code>, return <em>the </em><code>n<sup>th</sup></code><em> element of the <strong>count-and-say</strong> sequence</em>.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">n = 4</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">"1211"</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<pre>
 | 
						|
countAndSay(1) = "1"
 | 
						|
countAndSay(2) = RLE of "1" = "11"
 | 
						|
countAndSay(3) = RLE of "11" = "21"
 | 
						|
countAndSay(4) = RLE of "21" = "1211"
 | 
						|
</pre>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<div class="example-block">
 | 
						|
<p><strong>Input:</strong> <span class="example-io">n = 1</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io">"1"</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>This is the base case.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= n <= 30</code></li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<strong>Follow up:</strong> Could you solve it iteratively? |