您的位置:首页 > 其它

leetcode-Count and Say

2017-12-30 11:10 330 查看
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.

class Solution {
public String countAndSay(int n) {
if(n==1) return "1";
String str = countAndSay(n-1);
String res = new String();
for(int i=0; i<str.length();i++ ){
int count = 1;
while(i<str.length()-1 && str.charAt(i)==str.charAt(i+1)){
i++;
count++;
}
res = res+String.valueOf(count);
res = res+String.valueOf(str.charAt(i));
}
return res;
}
}


主要是要理解题目的意思,其次注意字符串连接末尾的情况,给出的程序处理的挺好的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: