Why does it take as much as 14ms. Any one gives me a 4ms solution which is the best in the chart.


  • 0
    A
    class Solution {
    public:
        string countAndSay(int n) {
            string pre = "1";
            while(--n)
            {
                int j = 0;
                string next;
                int i = pre.size() - 1;
                while(i >= 0)
                {
                
                    char ch = pre[i];
                    int cnt = 1;
                    i--;
                    while(i >= 0 && pre[i] == ch)
                    {
                        cnt++;
                        i--;
                    }
                    ostringstream ostr;
                    ostr<<cnt<<ch;
                next = ostr.str() + next;
                }
                pre = next;
            }
    
            return pre;
            
        }
    };

Log in to reply
 

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