author: Blankj blog : http://blankj.com time : 2017/05/03 desc :
| 9 | * </pre> |
| 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(); |
| 34 | for (int i = 1; i < 6; i++) { |
| 35 | System.out.println(solution.countAndSay(i)); |
| 36 | } |
| 37 | } |
| 38 | } |
nothing calls this directly
no outgoing calls
no test coverage detected