38:外观数列

This commit is contained in:
huangge1199 2021-10-15 13:41:43 +08:00
parent 80cc73d34b
commit f5e99eca7f
4 changed files with 166 additions and 2 deletions

View File

@ -0,0 +1,101 @@
//给定一个正整数 n 输出外观数列的第 n
//
// 外观数列是一个整数序列从数字 1 开始序列中的每一项都是对前一项的描述
//
// 你可以将其视作是由递归公式定义的数字字符串序列
//
//
// countAndSay(1) = "1"
// countAndSay(n) 是对 countAndSay(n-1) 的描述然后转换成另一个数字字符串
//
//
// 前五项如下
//
//
//1. 1
//2. 11
//3. 21
//4. 1211
//5. 111221
//第一项是数字 1
//描述前一项这个数是 1 1 记作 "11"
//描述前一项这个数是 11 1 记作 "21"
//描述前一项这个数是 21 2 + 1 记作 "1211"
//描述前一项这个数是 1211 1 + 2 + 1 记作 "111221"
//
//
// 描述 一个数字字符串首先要将字符串分割为 最小 数量的组每个组都由连续的最多 相同字符 组成然后对于每个组先描述字符的数量然后描述字符形成
//一个描述组要将描述转换为数字字符串先将每组中的字符数量用数字替换再将所有描述组连接起来
//
// 例如数字字符串 "3322251" 的描述如下图
//
//
//
//
//
//
// 示例 1
//
//
//输入n = 1
//输出"1"
//解释这是一个基本样例
//
//
// 示例 2
//
//
//输入n = 4
//输出"1211"
//解释
//countAndSay(1) = "1"
//countAndSay(2) = "1" = 1 = "11"
//countAndSay(3) = "11" = 1 = "21"
//countAndSay(4) = "21" = 2 + 1 = "12" + "11" = "1211"
//
//
//
//
// 提示
//
//
// 1 <= n <= 30
//
// Related Topics 字符串 👍 786 👎 0
package leetcode.editor.cn;
//38:外观数列
class CountAndSay {
public static void main(String[] args) {
//测试代码
Solution solution = new CountAndSay().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public String countAndSay(int n) {
String str = "1";
for (int i = 1; i < n; i++) {
String temp = "";
char ch = str.charAt(0);
int count = 1;
for (int j = 1; j < str.length(); j++) {
if (str.charAt(j) == ch) {
count++;
} else {
temp += "" + count + ch;
count = 1;
ch = str.charAt(j);
}
}
temp += "" + count + ch;
str = temp;
}
return str;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,63 @@
<p>给定一个正整数 <code>n</code> ,输出外观数列的第 <code>n</code> 项。</p>
<p>「外观数列」是一个整数序列,从数字 1 开始,序列中的每一项都是对前一项的描述。</p>
<p>你可以将其视作是由递归公式定义的数字字符串序列:</p>
<ul>
<li><code>countAndSay(1) = "1"</code></li>
<li><code>countAndSay(n)</code> 是对 <code>countAndSay(n-1)</code> 的描述,然后转换成另一个数字字符串。</li>
</ul>
<p>前五项如下:</p>
<pre>
1. 1
2. 11
3. 21
4. 1211
5. 111221
第一项是数字 1
描述前一项,这个数是 <code>1</code> 即 “ 一 个 1 ”,记作 <code>"11"
</code>描述前一项,这个数是 <code>11</code> 即 “ 二 个 1 ” ,记作 <code>"21"
</code>描述前一项,这个数是 <code>21</code> 即 “ 一 个 2 + 一 个 1 ” ,记作 "<code>1211"
</code>描述前一项,这个数是 <code>1211</code> 即 “ 一 个 1 + 一 个 2 + 二 个 1 ” ,记作 "<code>111221"</code>
</pre>
<p><strong>描述</strong> 一个数字字符串,首先要将字符串分割为 <strong>最小</strong> 数量的组,每个组都由连续的最多 <strong>相同字符</strong> 组成。然后对于每个组,先描述字符的数量,然后描述字符,形成一个描述组。要将描述转换为数字字符串,先将每组中的字符数量用数字替换,再将所有描述组连接起来。</p>
<p>例如,数字字符串 <code>"3322251"</code> 的描述如下图:</p>
<img alt="" src="https://pic.leetcode-cn.com/1629874763-TGmKUh-image.png" style="width: 581px; height: 172px;" />
<ul>
</ul>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 1
<strong>输出:</strong>"1"
<strong>解释:</strong>这是一个基本样例。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 4
<strong>输出:</strong>"1211"
<strong>解释:</strong>
countAndSay(1) = "1"
countAndSay(2) = 读 "1" = 一 个 1 = "11"
countAndSay(3) = 读 "11" = 二 个 1 = "21"
countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 30</code></li>
</ul>
<div><div>Related Topics</div><div><li>字符串</li></div></div><br><div><li>👍 786</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long