LeetCode 38. 外观数列
作者:互联网
答案:
public String countAndSay(int n) {
StringBuilder stringBuilder = new StringBuilder("1");
StringBuilder tempString = new StringBuilder();
for (int i = 1; i < n; i++) {
tempString.delete(0, tempString.length());
char curDigit = stringBuilder.charAt(0);
int digitCount = 1;
for (int j = 1; j < stringBuilder.length(); j++) {
//统计当前数字的个数
if (curDigit == stringBuilder.charAt(j)) {
digitCount++;
} else {
//变到另一个数字要重新统计
tempString.append(digitCount).append(curDigit);
curDigit = stringBuilder.charAt(j);
digitCount = 1;
}
}
tempString.append(digitCount).append(curDigit);
stringBuilder.delete(0, stringBuilder.length());
stringBuilder.append(tempString);
}
return stringBuilder.toString();
}
标签:38,数列,int,stringBuilder,curDigit,digitCount,tempString,LeetCode,append 来源: https://blog.csdn.net/m0_59675166/article/details/120782066