0ms C++ solution with stack


  • 0
    Z
    class Solution {
    public:
        string decodeString(string s) {
            stack<int> numS;
            stack<string> strS;
    		int i = 0;
    		numS.push(1);
    		strS.push("");
    		while(i < s.size()){
    			if(s[i] == ']'){
    				//update
    				int num = numS.top();
    				string str = strS.top();
    				numS.pop();
    				strS.pop();
    				string tmp;
    				for(int i = 0; i < num; ++i){
    					tmp += str;
    				}
    				strS.top() += tmp;
    				++i;
    			}
    			else if(isdigit(s[i])){
    				//push
    				int pos = s.find_first_of("[", i);
    				numS.push(stoi(s.substr(i, pos-i)));
    				strS.push("");
    				i = pos+1;
    			}
    			else{//is str //update 
    				int pos = s.find_first_of("123456789]", i);
    				strS.top() += s.substr(i, pos-i);
    				i = pos;
    			}
    		}
    		return strS.top();
    		
        }
    };
    

Log in to reply
 

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