题目传送地址: https://leetcode.cn/problems/count-and-say/
代码如下
//递归解法
public static String countAndSay(int n) {
if (n == 1) {
return "1";
}
String s = countAndSay(n - 1);
int left = 0;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < s.length() + 1; i++) {
char c = s.charAt(left);
if (i == s.length()) {
stringBuilder.append(i - left).append(c);
break;
}
char c1 = s.charAt(i);
if (c != c1) {
int count = i - left;
stringBuilder.append(count).append(c);
left = i;
}
}
return stringBuilder.toString();
}