| 10 | */ |
| 11 | public class Solution { |
| 12 | public String countAndSay(int n) { |
| 13 | String str = "1"; |
| 14 | while (--n > 0) { |
| 15 | int times = 1; |
| 16 | StringBuilder sb = new StringBuilder(); |
| 17 | char[] chars = str.toCharArray(); |
| 18 | int len = chars.length; |
| 19 | for (int j = 1; j < len; j++) { |
| 20 | if (chars[j - 1] == chars[j]) { |
| 21 | times++; |
| 22 | } else { |
| 23 | sb.append(times).append(chars[j - 1]); |
| 24 | times = 1; |
| 25 | } |
| 26 | } |
| 27 | str = sb.append(times).append(chars[len - 1]).toString(); |
| 28 | } |
| 29 | return str; |
| 30 | } |
| 31 | |
| 32 | public static void main(String[] args) { |
| 33 | Solution solution = new Solution(); |