给定一个经过编码的字符串,返回它解码后的字符串。
编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次。注意 k 保证为正整数。
你可以认为输入字符串总是有效的;输入字符串中没有额外的空格,且输入的方括号总是符合格式要求的。
此外,你可以认为原始数据不包含数字,所有的数字只表示重复的次数 k ,例如不会出现像 3a 或 2[4] 的输入。
示例:
s = "3[a]2[bc]", 返回 "aaabcbc".
s = "3[a2[c]]", 返回 "accaccacc".
s = "2[abc]3[cd]ef", 返回 "abcabccdcdcdef".
思路:
构建辅助栈 stack, 遍历字符串 s 中每个字符 c;
1.当 c 为数字时,将数字字符转化为数字 multi,用于后续倍数计算;
2.当 c 为字母时,在 res 尾部添加 c;
3.当 c 为 ‘[’ 时,将当前 multi 和 res 入栈,并分别置空置 0.
1)记录此 ‘[’ 前的临时结果 res 至栈,用于发现对应 ] 后的拼接操作;
2)记录此 ‘[’ 前的倍数 multi 至栈,用于发现对应 ] 后,获取 multi × […] 字符串。
3)进入到新 ‘[’ 后,res 和 multi 重新记录。
当 c 为 ‘]’ 时,stack 出栈,拼接字符串 res = last_res + cur_multi * res,其中:
last_res是上个 [ 到当前 [ 的字符串,例如 “3[a2[c]]” 中的 a;
cur_multi是当前 [ 到 ] 内字符串的重复倍数,例如 “3[a2[c]]” 中的 2。
返回字符串 res
例如3[a2[c]],算法过程:
1.首先 res为空,第一个字符为3,则repeatTime=3,再向后为’[’,于是入栈(3,"").同时,res置空,repeatTimes置为0.
2.遍历a res=‘a。
3.遍历到2,repeatTime=2.
4.遍历到’[’,入栈(2,“a”),同时,res置空,repeatTimes置为0.
5.遍历到c,res=c。
6.遍历到’]’,出栈,栈顶元素为(2,“a”),则res=“a”+res2=acc.
7.遍历到‘]’,出栈,栈顶元素为(3,""),则res=”“+res3=accaccacc
8.返回res。
AC代码:(C++)
class Solution {
public:
string repeat(const string& str, int times) {
//将字符串str重复times次
string res = "";
for (int i = 0; i < times; i++) {
res += str;
}
return res;
}
string decodeString(string s) {
int repeatTimes = 0;
string res = "";
stack<pair<int, string>> vecStack;
for (char i : s) {
if (i >= '0' && i <= '9')
repeatTimes = (repeatTimes * 10) + (i - '0');
else if (i == '[') {
vecStack.push(make_pair(repeatTimes, res));
res = "";
repeatTimes = 0;
} else if (i == ']') {
pair<int, string> temp = vecStack.top();
vecStack.pop();
res = temp.second + (temp.first == 0 ? "" : repeat(res, temp.first));
} else
res += i;
}
return res;
}
};