Java Solution


  • 0
    A
    class Solution {
        public String countAndSay(int n) {
            if (n == 1) {
                return "1";
            }
            char[] c = countAndSay(n - 1).toCharArray();
            int count = 0;
            char lastDigit = c[0];
            String res = "";
            for (int i = 0; i < c.length; i++) {
                if (c[i] == lastDigit) {
                    count++;
                } else {
                    res += "" + count + "" + lastDigit;
                    lastDigit = c[i];
                    count = 1;
                }
            }
            res += "" + count + "" + lastDigit;
            return res;
        }
    }
    

Log in to reply
 

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