https://www.cnblogs.com/skywang12345/p/string02.html
这道题算就是字符串处理的问题,序列中第一个字符串是“1”,接下来依次统计前一个字符串中连续相同字符的数量,并添加到下一字符串中。前15字符串如下(LeetCode上貌似只有18测试用例):
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
11131221133112132113212221
3113112221232112111312211312113211
1321132132111213122112311311222113111221131221
11131221131211131231121113112221121321132132211331222113112211
311311222113111231131112132112311321322112111312211312111322212311322113212221
import java.util.*;
public class Solution {
public String countAndSay(int n) {
String s[] = new String [n];
s[0] = "1";
for(int i=1;i<n;i++){//求第i个结果
StringBuilder str = new StringBuilder();
int len = s[i-1].length();
int count=1;
char c = s[i-1].charAt(0);
for(int j=1;j<len;j++){
if(s[i-1].charAt(j)==c){
count++;
}
else{
str.append(count).append(c);
count=1;
c = s[i-1].charAt(j);
}
}
str.append(count).append(c);//如果s[i-1]长度为1
s[i] = str.toString();
}
return s[n-1];
}
}