Share my C++ solution 0 ms iteration


  • 0
    W
    string countAndSay(int n) {
            string result = "1";
            if (n == 0) return "";
            int st;
            char c;
            string tmp;
            while (--n) {
                c = result[0];
                st = 0;
                tmp = "";
                for (int i=1; i<result.size(); ++i) {
                    if (result[i] != c) {
                        tmp.push_back('0' + i - st);
                        tmp.push_back(c);
                        st = i;
                        c = result[i];
                    }
                }
                tmp.push_back('0' + result.size() - st);
                tmp.push_back(c);
                result = tmp;
            }
            
            return result;
        }
    

Log in to reply
 

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