A sample c++ solution


  • 0
    L
    class Solution {
    public:
        string countAndSay(int n) {
            if(n==0)
                return "";
            string re="1",temp;
            int m;
            for(int i=1;i<n;i++)
            {
                m=1;
                temp="";
                for(int j=1;j<re.length()+1;j++)
                {
                    if(re[j-1]==re[j])
                    {
                        m++;
                    }
                    else
                    {
                        temp+=to_string(m)+re[j-1];
                        m=1;
                    }
                }
                re=temp;
            }
            return re;
        }
    };

Log in to reply
 

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