局域网小网站网站建设软件,手机端网站尺寸规范,北京网页设计公司兴田德润优惠,中国最近军事新闻视频1. 题目
给出一个字符串 s#xff08;仅含有小写英文字母和括号#xff09;。
请你按照从括号内到外的顺序#xff0c;逐层反转每对匹配括号中的字符串#xff0c;并返回最终的结果。
注意#xff0c;您的结果中 不应 包含任何括号。
示例 1#xff1a;
输入#xf…1. 题目
给出一个字符串 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 中只有小写英文字母和括号
我们确保所有括号都是成对出现的来源力扣LeetCode 链接https://leetcode-cn.com/problems/reverse-substrings-between-each-pair-of-parentheses 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。
2. 栈解题
class Solution {
public:string reverseParentheses(string s) {stackstring stk;string temp;for(int i 0; i s.size(); i){if(s[i] ! ( s[i] ! ))temp.push_back(s[i]);//字符串else if(s[i] (){stk.push(temp);//遇到左括号,前面字符串入栈temp ;//清空}else if(s[i] ))//遇到右括号反转当前字符串{reverse(temp.begin(), temp.end());temp stk.top()temp;//并将前面的连接起来stk.pop();//前面的栈内单词删除}}return temp;}
};