My run time beats 91.32% C++ solutions.(static vector)


  • 0
    W
    class Solution {
    public:
        string countAndSay(int n) {
            static vector<string> res(1,"1");
            //res[0] = "1";
            if(n == 1)  return res[0];
            if(n > res.size())
            {
                for(int i = res.size(); i < n; i++)
                {
                    string say;
                    int j = 0;
                   while(j < res[i-1].size())
                   {
                      char c = res[i-1][j];
                       int count = 0;
                       while(c == res[i-1][j])
                       {
                           count++;
                           j++;
                       }
                       say.push_back(count + '0');
                       say.push_back(c);
                   }
                    res.push_back(say);
                    
                }
            }
                return res[n-1];
        }
    };
    

Log in to reply
 

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