给出一个字符串 s(仅含有小写英文字母和括号)。
请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。
注意,您的结果中 不应 包含任何括号。
示例 1:
输入:s = “(abcd)”
输出:“dcba”
示例 2:
输入:s = “(u(love)i)”
输出:“iloveu”
示例 3:
输入:s = “(ed(et(oc))el)”
输出:“leetcode”
示例 4:
输入:s = “a(bcdefghijkl(mno)p)q”
输出:“apmnolkjihgfedcbq”
提示:
0 <= s.length <= 2000
s 中只有小写英文字母和括号
我们确保所有括号都是成对出现的
class Solution {
public:
stacks1;
queues2;
bool isalpha(char a){
return a >= 'a' && a <= 'z';
}
string reverseParentheses(string s) {
for(auto &a : s){
if(isalpha(a) || a == '('){
s1.push(a);
}
else{
while(s1.top() != '('){
s2.push(s1.top());
s1.pop();
}
s1.pop();
while(s2.size()){
s1.push(s2.front());
s2.pop();
}
}
}
string t = "";
while(s1.size()){
t.append(1,s1.top());
s1.pop();
}
reverse(t.begin(),t.end());
return t;
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/137003.html