The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
package com.wyt.leetcodeOJ;
/**
* @author wangyitao
* @Date 2015-01-23
* @version
* @Description The count-and-say sequence is the sequence of integers beginning
* as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as
* "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off
* as "one 2, then one 1" or 1211. Given an integer n, generate the
* nth sequence. Note: The sequence of integers will be represented
* as a string.
*
*/
public class CountandSay {
public static void main(String[] args) {
System.out.println(countAndSay(6));
}
public static String countAndSay(int n) {
String str = "1";
if (n == 1) {
return str;
}
for (int i = 2; i <= n; i++) {
str = getTmp(str);
}
return str;
}
public static String getTmp(String nstr) {
String str = "";
char tmp;
tmp = nstr.charAt(0);
int count = 0;
for (int i = 0; i < nstr.length(); i++) {
if (nstr.charAt(i) == tmp) {
count++;
} else {
str += count;
str += tmp;
tmp = nstr.charAt(i);
count = 1;
}
}
str += count;
str += tmp;
return str;
}
}
本文详细解析了Count and Say序列的生成过程,通过代码实现展示了如何从给定整数n生成对应的序列。该算法涉及到字符串操作和计数逻辑,适合对数据结构和算法有深入理解的学习者。
494

被折叠的 条评论
为什么被折叠?



