My 2ms C++ solution


  • -1
    Y
        string countAndSay(int n) {
        if(n==0)  return " ";
        if(n==1)  return "1";
        string str="1";
        string tmp;
        for(int i=1;i<n;++i)
        {
            tmp.clear();
            int cnt=1;
            for(int j=0;j<str.size();++j)
            {
                while(j+1<str.size())
                {
                    if(str[j]==str[j+1])
                    {
                        ++cnt;
                        ++j;
                    }
                    else
                    break;
                }
                tmp.push_back(cnt+'0');
                tmp.push_back(str[j]);
                cnt=1;
            }
            str=tmp;
        }
        return str;
    }

Log in to reply
 

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