题目
题目链接
题解
常规解法:
class Solution {
public String countAndSay(int n) {
String res = "1";
for(int i = 1; i < n; i++) {
res = describe(res);
}
return res;
}
private String describe(String described) {
if (described == null || described.length() == 0) {
return described;
}
StringBuilder des = new StringBuilder();
for (int i = 0, j = 0; j < described.length(); j++) {
if (described.charAt(i) != described.charAt(j)) {
des.append(merge(described.substring(i, j)));
i = j;
}
// 边界处理
if (j == described.length() - 1) {
des.append(merge(described.substring(i, j + 1)));
}
}
return des.toString();
}
private String merge(String d) {
return d.length() + d.substring(0, 1);
}
}
打表解法:没啥意思不写了