public class Solution {
public String countAndSay(int n) {
if (n <= 0) {
return "";
}
String res = "1";
int num = 1;
for (int i = 0; i < n - 1; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < res.length(); j++) {
if (j + 1 < res.length() && res.charAt(j) == res.charAt(j + 1)) {
num++;
} else {
sb.append(num + "" + res.charAt(j));
num = 1;
}
}
res = sb.toString();
}
return res;
}
}
Count and Say
最新推荐文章于 2024-12-21 10:31:21 发布