注:七月在线听课笔记
s = "3[a]2[bc]", return "aaabcbc". s = "3[a2[c]]", return "accaccacc". s = "2[abc]3[cd]ef", return "abcabccdcdcdef".
参考代码有些问题,先记录下来,回头再思考。
class Solution {
public:
string decodeString(string s) {
stack<string> chars;
stack<int> nums;
string res;
int num = 0;
for(char c : s){
if(isdigit(c))
num = num * 10 + (c - '0');//????
else if(isalpha(c)){
res += c;
}else if(c == '['){
chars.push(res);
nums.push(num);
res = "";
num = 0;
}else {
string temp = res;
for(int i = 0;i < nums.top() - 1;++i)
res += temp;
res = chars.top() + res;
chars.pop();
nums.pop();
}
}
return res;
}
};
结果:通过