Share my solution


  • 0
    X
    class Solution {
    public:
        string countAndSay(int n) {
            string ret = "1";
            for (int i = 2; i <= n; ++i) {
                ostringstream stream;
                int j = 0, k = 1;
                for (; k < ret.size(); ++k) {
                    if (ret[j] != ret[k]) {
                        stream << to_string(k - j);
                        stream << ret[j];
                        j = k;
                    }
                }
                stream << to_string(k - j);
                stream << ret[j];   
                stream.str().swap(ret);
            }
            return ret;
        }
    };
    

Log in to reply
 

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