forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (27 loc) · 915 Bytes
/
Solution.java
File metadata and controls
30 lines (27 loc) · 915 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
import java.util.*;
public class Solution {
public String countAndSay(int n) {
if (n < 0) return "";
char[] original = new char[] { '1' };
StringBuilder sb = null;
for (int i = 1; i < n; i++) {
sb = new StringBuilder();
int len = original.length;
int begin = 0;
for (int j = 1; j <= len; j++)
if (j == len || original[j] != original[begin]) {
sb.append(String.format("%d", (j - begin)));
sb.append(original[begin]);
begin = j;
}
original = sb.toString().toCharArray();
}
return new String(original);
}
public static void main(String[] args) {
Solution s = new Solution();
for (int i = 0; i < 10; ++i) {
System.out.format("%d\t%s\n", i, s.countAndSay(i));
}
}
}