leetcode ----394. Decode String

来源:互联网 发布:js怎么关闭手机端页面 编辑:程序博客网 时间:2024/05/17 08:01
string decodeString(string s)     {        string tem;        stack<int> stanum; //存储数字        stack<string> stastr;//存储字符串        int n=0;        for(int i=0;i<s.length();i++)        {            if(isdigit(s[i]))//判断当前字符是否为0-9数字            {                n = n*10+s[i]-'0';//计算当前数字            }            else if(s[i]=='[')            {                stanum.push(n);//循环次数进栈                n=0;//初始化n                stastr.push(tem);//上一次的tem进栈作为top                tem.clear();//清空tem            }            else if(s[i]==']')            {                int k = stanum.top();                stanum.pop();                for(;k>0;k--)                {                    stastr.top()+=tem;//top累加k次的tem                }                tem = stastr.top();//把stastr的top赋值给tem                stastr.pop();            }            else                 tem+=s[i];//tem一直累加        }        return tem;    }

0 0