Share my simple Java Solution


  • 0
    public class Solution {
        public String countAndSay(int n) {
            String str = "1";
            while (--n > 0) {
                int times = 1;
                StringBuilder sb = new StringBuilder();
                char[] chars = str.toCharArray();
                int len = chars.length;
                for (int j = 1; j < len; j++) {
                    if (chars[j - 1] == chars[j]) {
                        times++;
                    } else {
                        sb.append(times).append(chars[j - 1]);
                        times = 1;
                    }
                }
                str = sb.append(times).append(chars[len - 1]).toString();
            }
            return str;
        }
    }
    

    I think u can understand it simply.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.