-
Notifications
You must be signed in to change notification settings - Fork 0
/
394. 字符串解码.cc
35 lines (33 loc) · 871 Bytes
/
394. 字符串解码.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
string decodeString(string s)
{
string res;
if(s.empty()) return res;
stack<int> numSta;
stack<string> resSta;
int size = s.size();
int num = 0;
for(int i=0; i<size; ++i)
{
if(isalpha(s[i])) res.push_back(s[i]);
else if(isdigit(s[i])) num = num*10 + s[i] - '0';
else if(s[i] == '[')
{
numSta.push(num);
num = 0;
resSta.push(res);
res.clear();
}
else if(s[i] == ']')
{
int times = numSta.top();
numSta.pop();
for(int k=0; k<times; ++k) resSta.top() += res;
res = resSta.top();
resSta.pop();
}
}
return res;
}
};