My 2ms c++ code


  • 0
    L
     class Solution {
    public:
        string countAndSay(int n) {
            if(n == 0)return "0";
            string temp("1");
            int i = 1;
            while(i++ < n){
                int index = 0;
                int next = index;
                string t;
                while(next < temp.size()){
                     while(next < temp.size() && temp[next] == temp[index])next++;
                     t.push_back(next - index + '0');
                     t.push_back(temp[index]);
                     index = next;
                }
               temp = t;
            }
            return temp;
        }
    };

Log in to reply
 

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