C++ solution, easy understand


  • 0
    Y
    string countAndSay(int n) {
        string res = "1";
        for (int i = 2; i <= n; ++i) {
            int k = 0, count = 0;
            string worker;
            for (int j = 0; j < res.size(); ++j) {
                if (res[k] == res[j]) {
                    ++count;
                } else {
                    worker += to_string(count) + res[k];
                    k = j--;
                    count = 0;
                }
            }
            res = worker + to_string(count) + res[res.size() - 1];
        }
        return res;
    }

Log in to reply
 

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