public String countAndSay(int n) {
if (n==0) return "";
if (n==1) return "1";
String lastResult = countAndSay(n-1);
StringBuilder sb = new StringBuilder();
for (int i=0; i<lastResult.length();) {
char currChar = lastResult.charAt(i);
int count = 0;
while(i<lastResult.length() && currChar == lastResult.charAt(i)) {
count++;
i++;
}
sb.append(count).append(currChar);
}
return sb.toString();
}
No comments:
Post a Comment