您的位置:首页 > 其它

leetcode解题方案--038--count and say

2017-11-21 16:35 309 查看

题目

The count-and-say sequence is the sequence of integers with the first five terms as following:

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.

后一个字符串是前一个字符串的读法

public static String countAndSay(int n) {
if (n<1) {
return "";
}
if (n==1) {
return "1";
}else {
char[] str = countAndSay(n-1).toCharArray();
int count = 1;
char curr = ' ';
StringBuffer xx = new StringBuffer("");
for (int i = 0;i<str.length;i++) {
if (str[i]!=curr) {
if (curr!=' ') {
xx.append(count);
xx.append(curr);
}
count = 1;
curr = str[i];
} else {
count++;
}
}
xx.append(count);
xx.append(curr);
return xx.toString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: