Leetcode394. 字符串解码
class Solution {
public:
string dfs(string s,int &idx)
{
string str;
while(idx<s.size())
{
if(s[idx]==']')
{
idx++;
return str;
}
else if(s[idx]>'0'&&s[idx]<='9')
{
string num;
while(s[idx]>='0'&&s[idx]<='9') num+=s[idx++];
if(s[idx]=='[')
{
int cnt=stoi(num);
string t=dfs(s,++idx);
while(cnt--)
str=str+t;
}
}
else
str+=s[idx++];
}
return str;
}
string decodeString(string s) {
int u=0;
return dfs(s,u);
}
};
有帮助的话可以点个赞,我会很开心的~