How about like this? any better?


  • 0
    A
    string countAndSay(int n) {
        string s1;
        if( n == 0) return s1;
        string s2;
        s2 += "1";
        int count = 1;
        int pos = 0;
        stringstream ss;
        string temp;
        while(n > 1){//record iterate count
            s1 = s2;
            s2.clear();
            pos = 0;
            count = 1;
            while(pos < s1.length()){
                while((pos+count < s1.length()) && (s1[pos] == s1[pos+count]))
                    count++;
                ss.clear();
                ss << count;
                ss >> temp;
                s2 += temp;
                s2.append(1,s1[pos]);
                pos = pos + count;
                count = 1;
            }
            n--;
        }
        return s2;
    }

Log in to reply
 

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