-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0394.cpp
More file actions
31 lines (29 loc) · 776 Bytes
/
Copy path0394.cpp
File metadata and controls
31 lines (29 loc) · 776 Bytes
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
class Solution {
public:
string decodeString(string s) {
stack<int> is;
stack<string> ss;
ss.push("");
for (int i = 0; i < s.size(); i++) {
if (isdigit(s[i])) {
int res = 0;
do {
res *= 10;
res += s[i] - '0';
} while (isdigit(s[++i]));
is.push(res);
ss.push("");
} else if (s[i] == ']') {
string res = "";
while (is.top()--)
res += ss.top();
is.pop();
ss.pop();
ss.top() += res;
} else {
ss.top() += s[i];
}
}
return ss.top();
}
};