1、前言
题目描述2、思路
就是按照题目的步骤,从最小的单元开始,一步步增大
3、代码
class Solution {
public String countAndSay(int n) {
if (n == 1) {
return "1";
}
String num = countAndSay(n - 1);
StringBuilder sb = new StringBuilder();
int count = 0;
for (int i = 0; i < num.length(); i++) {
if(i + 1 < num.length() && num.charAt(i) == num.charAt(i + 1)){
count++;
continue;
}
sb.append(count + 1).append(num.charAt(i));
count = 0;
}
return sb.toString();
}
}
网友评论