-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecodeString.cpp
41 lines (31 loc) · 968 Bytes
/
decodeString.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<int> numStack;
stack<string> strStack;
string str = "";
int num = 0;
for(char c: s) {
if(isdigit(c)) {
num = num * 10 + (c - '0');
} else if(c == '[') {
strStack.push(str);
str = "";
numStack.push(num);
num = 0;
} else if(c == ']') {
string temp = str;
str = strStack.top();
strStack.pop();
int count = numStack.top();
numStack.pop();
while(count-- > 0) {
str.append(temp);
}
} else {
str += c;
}
}
return str;
}
};