-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path394-decode-string.cpp
41 lines (41 loc) · 1.19 KB
/
394-decode-string.cpp
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
36
37
38
39
40
41
class Solution {
public:
string decodeString(string s) {
stack <string> stk;
string sub, word, num;
for (int i=0; i<s.length(); i++){
// cout << s[i];
if (s[i] == ']'){
sub = "";
word = "";
while(stk.top() != "["){
sub = stk.top() + sub;
stk.pop();
cout << "$ " << sub << endl;
};
stk.pop();
num = "";
while (stk.top() <= "9" && stk.top()>="0"){
num = stk.top() + num;
cout << "$ " << num << endl;
stk.pop();
if (stk.empty()) break;
}
cout << endl <<num;
int rep = stoi(num);
while (rep--)
word += sub;
stk.push(word);
cout << "# " << word << endl;
continue;
}
stk.push(string(1,s[i]));
}
string res = "";
while (!(stk.empty())){
res = stk.top() + res;
stk.pop();
}
return res;
}
};