公司网站建设/沈阳网站seo公司
题目描述
给出一个字符串 s(仅含有小写英文字母和括号)。
请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。
注意,您的结果中 不应 包含任何括号
示例 :
(u(love)i)
iloveu
(ed(et(oc))el)
leetcode
提示:
字符串长度在10000以内
s 中只有小写英文字母和括号
我们确保所有括号都是成对出现的
#include <iostream>
#include <stack>
#include <queue>using namespace std;int main() {string str;cin >>str;int n=str.size(); //不包括最后的'/0'stack<char> s1;queue<char> q1;stack<char> s2;for(int i=0;i<n;i++){if(str[i]!=')'){s1.push(str[i]);}else{while (true){char t =s1.top();s1.pop();if (t=='('){break;} //end ifq1.push(t);}//end whilewhile(!q1.empty()){s1.push(q1.front());q1.pop();}}//end else}//end forwhile (!s1.empty()){s2.push(s1.top());s1.pop();}while (!s2.empty()){cout << s2.top();s2.pop();}return 0;
}