-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394_Decode-String.cpp
More file actions
54 lines (49 loc) · 1.32 KB
/
394_Decode-String.cpp
File metadata and controls
54 lines (49 loc) · 1.32 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
#include <string>
#include <stack>
#include <algorithm>
#include <cctype>
using namespace std;
class Solution {
public:
string decodeString(string s) {
stack<string> ss;
stack<int> cnt;
string ans;
int l = s.size();
for (int i = 0; i < l; ++i)
{
if (isdigit(s[i]))
{
int left = i;
while (isdigit(s[i + 1]))
{
++i;
}
ss.push(ans);
cnt.push(stoi(s.substr(left, i - left + 1))); //裁剪字符串
ans.clear();
i++;
continue;
}
if (s[i] == ']')
{
int t = cnt.top();
string st = ans;
cnt.pop();
for(int j = 0; j < t - 1; ++j) //ans本身算一次
{
ans += st;
}
string before = ss.top();
ss.pop();
ans = before + ans;
continue;
}
ans += s[i];
}
return ans;
}
};
//遇到数字时,找出数字是几位数字,然后裁剪字符串
// 遇到'[',入栈,清空字符串
// 遇到']',出栈,加入到前字符串的后端,重复该过程