class Solution {
public:
string decodeString(string s) {
string ans = "";
int n = s.length();
int count = 0;
stack<pair<int, int>> ss;
for(auto x : s){
if(isdigit(x)){
count = 10 * count + (x - '0');
}
else if(x == '['){
ss.push({count, ans.size()});
count = 0;
}
else if(isalpha(x)){
ans += x;
}
else if(x == ']'){
int n = ss.top().first;
string str = ans.substr(ss.top().second, ans.size() - ss.top().second);
for(int i = 0; i < n - 1; ++i){
ans += str;
}
ss.pop();
}
}
return ans;
}
};
Leecode394 字符串解码
最新推荐文章于 2024-10-05 12:29:44 发布