-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode_394.cpp
More file actions
40 lines (36 loc) · 923 Bytes
/
Copy pathLeetCode_394.cpp
File metadata and controls
40 lines (36 loc) · 923 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
32
33
34
35
36
37
38
39
40
//Decode String
class Solution {
public:
string decodeString(string s) {
stack<int>nums;
stack<string>str;
int num=0;
string currStr="";
for(char i : s){
if(i>='0' && i<='9'){
num=num*10+(i-'0');
}
else if (i == '['){
nums.push(num);
num=0;
str.push(currStr);
currStr="";
}
else if (i==']'){
int multi = nums.top();
nums.pop();
string prev = str.top();
str.pop();
string repeted = "";
for(int i=0;i<multi;i++){
repeted+=currStr;
}
currStr=prev+repeted;
}
else{
currStr+=i;
}
}
return currStr;
}
};