The recursive method makes the code simpler.


  • 0
    L

    The recursive method always makes the code simpler. 6ms Accepted.

    class Solution {
    public:
        string countAndSay(int n) {
            if (n == 1) return "1";
            string last = countAndSay(n - 1);
            string ret;
            for (int i = 0; i < last.size(); ) {
                int j = i;
                while (++i < last.size() && last[j] == last[i]) {}
                ret += to_string(i - j) + last[j];
            }
            return ret;
        }
    };
    

Log in to reply
 

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