-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecodeString.cpp
39 lines (36 loc) · 961 Bytes
/
DecodeString.cpp
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
#include <string>
using namespace std;
class Solution {
public:
/* Sol 0
* 递归,如果遇到数字,就递归处理下一个字符串,遇到]就返回
*/
string decodeString(string s) {
int pos = 0;
return decodeString(s,pos);
}
string decodeString(string &s,int &pos){
string str;
while(pos < s.length() && s[pos] != ']'){
if(!digit(s[pos])) str.push_back(s[pos++]);
else{
int n = 0;
while(digit(s[pos])){
n = n * 10 + s[pos++] - '0';
}
++pos; // [
string tmp = decodeString(s,pos);
++pos; // ]
while(n > 0){
str += tmp;
--n;
}
}
}
return str;
}
bool digit(char c){
if(c >= '0' && c <= '9') return true;
else return false;
}
};