class Solution { public: string decodeString(string s) { stack<int> nums; stack<string> ch; int num = 0; string str = ""; for(auto c:s){ if(isdigit(c)) num = num*10 + (c-‘0‘); else if(isalpha(c)) str += c; else if(c==‘[‘){ ch.push(str); nums.push(num); str = ""; num = 0; } else{ string tmp = str; for(int i=0; i<nums.top()-1; ++i) //本身一次已经在内,故nums.top()-1 str += tmp; //这里字符相加的顺序不能颠倒 str = ch.top()+str; nums.pop(); ch.pop(); } } return str; } };
原文:https://www.cnblogs.com/Bella2017/p/11173459.html