2ms Share my C++ solution


  • 1
    D
    string countAndSay(int n) {
        string ans("1");
        if (n == 1)
            return ans;
    
        while (--n) {
            string temp = ans;
            ans.clear();
            int count = 1;
            for (int i = 0; i < temp.size(); i++) {
                if (temp[i] == temp[i + 1])
                    count++;
                else {
                    ans.push_back(count + '0');
                    ans.push_back(temp[i]);
                    count = 1;
                }
            }
        }
        return ans;
    }

Log in to reply
 

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