0
点赞
收藏
分享

微信扫一扫

login登录界面

天行五煞 03-22 10:00 阅读 2

维护一个放数字的栈,一个放字母的栈

遇到[把数字和字母入栈,遇到]把当前字母循环加上数字栈头遍的字母栈头

class Solution {
public:
    string decodeString(string s) {
     string ans="";
     stack<int>sz;
 stack<string>zm;里面是string 还是Char,要看你定义往里加的字符是哪个类型
     int count=0;
     for(auto& x:s){
        if(isdigit(x))
            count=10*count+x-'0';
        else if(x=='['){
            sz.push(count);
            count=0;
            zm.push(ans);
            ans="";     }
        else if(isalpha(x))
        ans+=x;
        else if(x==']'){
            int n=sz.top();
            string cur=zm.top();
            sz.pop();
            zm.pop();
            while(n--){
                cur+=ans;   }
            ans=cur;}    }
     return ans;}};
举报

相关推荐

0 条评论