您的位置:首页 > 其它

leetcode ----394. Decode String

2016-09-25 16:27 441 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string stack