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

Method countAndSay

src/com/blankj/easy/_0038/Solution.java:12–30  ·  view source on GitHub ↗
(int n)

Source from the content-addressed store, hash-verified

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();

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected