MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0038/Solution.java:11–38  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/05/03 desc :

Source from the content-addressed store, hash-verified

9 * </pre>
10 */
11public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected