这种字符串处理的题目,微软很喜欢考
思路是在递归的基础上进行字符串处理
class Solution {
public String countAndSay(int n) {
if(n==1) return "1";
String str = countAndSay(n-1);
StringBuilder sb = new StringBuilder();
int count = 1, i = 0;
while(i<str.length()){
while(i+1<str.length()&&str.charAt(i)==str.charAt(i+1)){
count++;
i++;
}
sb.append(count);
sb.append(str.charAt(i));
i++;
count = 1;
}
return sb.toString();
}
}