C++ 0ms stack way


  • 1
    L
        string decodeString(string s) {
            string res="";
            for(char c:s){
                if(c!=']'){
                    res.push_back(c);
                }else{
                    string str="";
                    while(res.back()!='['){
                        str.insert(str.begin(),res.back());
                        res.pop_back();
                    }
                    
                    res.pop_back();
                    string countstr="";
                    while(res.size()>0 && res.back()>='0' && res.back()<='9'){
                        countstr.insert(countstr.begin(),res.back());
                        res.pop_back();
                    }
                    int count=std::stoi(countstr);
                    string newstr="";
                    for(int i=0;i<count;i++){
                        newstr.insert(newstr.end(),str.begin(),str.end());
                    }
                    
                    res.insert(res.end(),newstr.begin(),newstr.end());
                }
            }
            return res;
        }
    

Log in to reply
 

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