Skip to content

Commit eb3c7a8

Browse files
38. Count and Say (java)
1 parent 2bc5898 commit eb3c7a8

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public String countAndSay(int n) {
3+
String one = "1";
4+
while (n > 1) {
5+
one = say(one);
6+
n--;
7+
}
8+
return one;
9+
}
10+
11+
private String say(String las) {
12+
StringBuilder sBuilder = new StringBuilder();
13+
int l = 1;
14+
for (int i = 0; i < las.length(); i++) {
15+
if (i < las.length() - 1 && las.charAt(i) == las.charAt(i + 1)) l++;
16+
else {
17+
sBuilder.append(l).append(las.charAt(i));
18+
l = 1;
19+
}
20+
}
21+
return sBuilder.toString();
22+
}
23+
}

0 commit comments

Comments
 (0)