Concise C++ Answer


  • 1
    L
    string countAndSay(int n) 
    {
        string cur;
        if(n < 2)
            return "1";
        string pre = "11";
        while(n - 2)
        {
            for(int i = 0, count; i < pre.size(); i += count)
            {
                count = 1;
                while(i + count < pre.size() && pre[i] == pre[i + count])
                    count++;
                cur.push_back(count + '0');
                cur.push_back(pre[i]);
            }
            n--;
            pre = cur;
            cur.clear();
        }
        return pre;
    }

Log in to reply
 

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