-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountAndSay.java
More file actions
38 lines (36 loc) · 1.06 KB
/
CountAndSay.java
File metadata and controls
38 lines (36 loc) · 1.06 KB
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
32
33
34
35
36
37
38
public class CountAndSay {
/**
* 1. 1
* 2. 11
* 3. 21
* 4. 1211
* 5. 111221
*/
public static String countAndSay(int n) { // n = 5
int i = 1;
String res = "1";
while (i < n) {
int count = 0;
StringBuilder sb = new StringBuilder();
char c = res.charAt(0); // c = "1"
for (int j = 0; j <= res.length(); j++) {
if (j != res.length() && res.charAt(j) == c) {
count++; // count = 1
} else {
sb.append(count); // sb = 1
sb.append(c); // sb = 11
if (j != res.length()) {
count = 1;
c = res.charAt(j);
}
}
}
res = sb.toString(); // res = "11"
i++; // i = 2
}
return res;
}
public static void main(String[] args) {
System.out.println(countAndSay(5));
}
}