-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394_Decode_String.cpp
More file actions
60 lines (53 loc) · 1.54 KB
/
Copy path394_Decode_String.cpp
File metadata and controls
60 lines (53 loc) · 1.54 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 100%faster
class Solution {
public:
string decodeString(string s) {
stack<int>is;
stack<char>cs;
for(int i=0;i<s.size();i++){
if(isdigit(s[i])){
int sum = 0;
while(isdigit(s[i])){
sum = sum*10 + s[i]-'0';
i++;
}
i--;
is.push(sum);
}
else if(s[i]=='['){
if(i==0 || !isdigit(s[i-1])){
is.push(1);
}
cs.push(s[i]);
}
else if(s[i]==']'){
string temp="";
while(!cs.empty() && cs.top()!='['){
temp.push_back(cs.top());
cs.pop();
}
if(!cs.empty())
cs.pop();
int x = is.top();
is.pop();
cout<<temp<<x<<endl;
reverse(temp.begin(),temp.end());
for(int j=0;j<x;j++){
for(int k=0;k<temp.size();k++){
cs.push(temp[k]);
}
}
cout<<cs.size()<<endl;
}
else
cs.push(s[i]);
}
string str="";
while(!cs.empty()){
str.push_back(cs.top());
cs.pop();
}
reverse(str.begin(),str.end());
return str;
}
};