(栈)leetcode 394

匿名 (未验证) 提交于 2019-12-02 23:47:01

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;     } };

 

转载请标明出处:(栈)leetcode 394
文章来源: (栈)leetcode 394
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!