-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path394.cpp
More file actions
50 lines (46 loc) · 1.43 KB
/
Copy path394.cpp
File metadata and controls
50 lines (46 loc) · 1.43 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
#include<stdio.h>
#include<string>
using namespace std;
class Solution {
public:
string decodeString(string s) {
string ret;
for (int i = 0; i < s.length(); i++) {
int repeat = 0;
char c = s.at(i);
if (c >= '0' && c <= '9') {
while (c >= '0' && c <= '9') {
repeat = repeat * 10 + c - '0';
//printf("repate=%d, i=%d\n", repeat, i);
i++;
c = s.at(i);
}
int brackets = 1;
int j = i + 1;
while (j < s.length() && (brackets > 0)) {
char c2 = s.at(j);
if (c2 == '[') brackets++;
if (c2 == ']') brackets--;
j++;
}
string s2(s, i+1, j-i-2);
//printf("repeat=%d, s2=%s\n", repeat, s2.c_str());
i = j-1;
while (repeat > 0) {
repeat--;
ret += decodeString(s2);
}
} else {
ret += c;
}
}
return ret;
}
};
int main(int argc, char** argv)
{
Solution s;
string in(argv[1]);
printf("ret=%s\n", s.decodeString(in).c_str());
return 0;
}