-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountandSay.java
More file actions
26 lines (24 loc) · 636 Bytes
/
Copy pathCountandSay.java
File metadata and controls
26 lines (24 loc) · 636 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
class Solution {
public String countAndSay(int n) {
if (n <= 0) return "";
String st = "1";
String result = "";
while (n > 1) {
char x = st.charAt(0);
int cnt = 1;
for (int i = 1; i < st.length(); i++) {
if (st.charAt(i) == x) cnt++;
else {
result = result + cnt + x;
x = st.charAt(i);
cnt = 1;
}
}
result = result + cnt + x;
st = result;
result = "";
n--;
}
return st;
}
}