Skip to content

Commit d27148b

Browse files
Add files via upload
1 parent ceb2bd0 commit d27148b

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Decode String/Decode_String.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// 典型的深度优先搜索,其实并没啥
2+
// Runtime: 0 ms, faster than 100.00% of C++ online submissions for Decode String.
3+
// Memory Usage: 8.9 MB, less than 84.31% of C++ online submissions for Decode String.
4+
5+
class Solution
6+
{
7+
public:
8+
string decodeString(string input)
9+
{
10+
if (input.length() == 0)
11+
return "";
12+
else
13+
{
14+
int index = 0;
15+
return getstring(input, index);
16+
}
17+
}
18+
private:
19+
string getstring(const string& input, int& i)
20+
{
21+
bool flag = false;
22+
if (input[i] == '[')
23+
{
24+
++i;
25+
flag = true;
26+
}
27+
28+
string res = "";
29+
while (i < input.length() && ((flag && input[i] != ']') || (flag == false)))
30+
{
31+
if (input[i] >= '0' && input[i] <= '9')
32+
{
33+
int times = 0;
34+
while (input[i] >= '0' && input[i] <= '9')
35+
{
36+
times *= 10;
37+
times += input[i++] - '0';
38+
}
39+
string temp = getstring(input, i);
40+
++i;
41+
42+
for (int j = 0; j < times; ++j)
43+
res += temp;
44+
}
45+
else
46+
res += input[i++];
47+
}
48+
return res;
49+
}
50+
};

0 commit comments

Comments
 (0)