-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
31 lines (29 loc) · 793 Bytes
/
Solution.java
File metadata and controls
31 lines (29 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package leetcode._38_;
class Solution {
public String countAndSay(int n) {
String tmp = "1";
if (n == 1) {
return tmp;
}
for (int i = 1; i < n; i++) {
tmp = say(tmp);
}
return tmp;
}
private String say(String numStr) {
char tmp = numStr.charAt(0);
int counter = 1;
StringBuilder sb = new StringBuilder();
for (int i = 1; i < numStr.length(); i++) {
if (numStr.charAt(i) == numStr.charAt(i - 1)) {
counter++;
} else {
sb.append(counter).append(tmp);
tmp = numStr.charAt(i);
counter = 1;
}
}
sb.append(counter).append(tmp);
return sb.toString();
}
}