【问题】给出一个字符串 s(仅含有小写英文字母和括号)。

请你按照从括号内到外的顺序,逐层反转每对匹配括号中的字符串,并返回最终的结果。

注意,您的结果中 不应 包含任何括号。

示例 :
输入:s = "(abcd)"
输出:"dcba"
示例 :
输入:s = "(u(love)i)"
输出:"iloveu"
示例 :
输入:s = "(ed(et(oc))el)"
输出:"leetcode"
示例 :
输入:s = "a(bcdefghijkl(mno)p)q"
输出:"apmnolkjihgfedcbq"

提示:

0 <= s.length <= 2000

s 中只有小写英文字母和括号
我们确保所有括号都是成对出现的

【题解】

class Solution {
public:
string reverseParentheses(string s) {
int len = s.length();
stack<int> sk;
for(int i=;i<len;++i){
char c = s[i];
if(c == '(') sk.push(i);
else if(c == ')'){
auto it = sk.top();
sk.pop();
reverse(s.begin()+it+,s.begin()+i);
}
}
auto it = s.begin(),e = s.end();
while(it!=e){
if(*it=='(' || *it==')') it = s.erase(it);
else ++it;
}
return s;
}
};

【LeetCode】反转每对括号间的子串的更多相关文章

  1. LeetCode:有效的括号【20】

    LeetCode:有效的括号[20] 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. ...

  2. 每日一道 LeetCode (48):最长回文子串

    每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...

  3. [LeetCode] Score of Parentheses 括号的分数

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  4. [LeetCode] 20. 有效的括号 (栈)

    思路: 首先用字典将三对括号存储,遍历字符串中每个字符,遇到左括号就入栈:遇到右括号就开始判断:是否与栈弹出的顶字符相同. 如果到最后栈被清空,说明全部匹配上了,为真. class Solution( ...

  5. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  6. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. LeetCode 28 Implement strStr() (实现找子串函数)

    题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description   Problem : 实现找子串的操作:如果没有找到则返回 ...

  8. leetcode 最长有效括号

    给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()&quo ...

  9. LeetCode Golang 5. 最长回文子串

    5. 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab&quo ...

随机推荐

  1. (转)js实现倒计时效果(年月日时分秒)

    原文链接:http://mengqing.org/archives/js-countdown.html 之前做的活动页面很多都用到了倒计时功能,所以整理下下次直接用.(用的是张鑫旭写的一个倒计时,稍作 ...

  2. IDEA导入项目后,导入artifacts 方法 以及 Spring的配置文件找不到的解决方法

    我们一般选择 open 项目,如果没有artifacts 的添加选项,我们就要选择 import 项目. 如果没有artifacts ,项目下面会有错误提示,点击错误提示Fix,设置里面导入artif ...

  3. 学习SpringMVC 文件上传 遇到的问题,403:returned a response status of 403 Forbidden ,409文件夹未找到

    问题一: 409:文件夹没有创建好,找不到指定的文件夹,创建了即可. 问题二: 403:returned a response status of 403 Forbidden 我出现这个错误的原因是因 ...

  4. Android之Context和Activity互相转换

    1.context转换为activity Activity activity = (Activity) context; 2.从activity得到context 在activity的方法中用cont ...

  5. 项目启动异常,java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo ...

  6. Vmware tools变灰不能点击的问题

    1. 挂载镜像文件,虚拟机->设置->硬件->CD/DVD.右边“连接”下面选择“使用IOS镜像文件”,浏览选择虚拟机包目录下面linux.iso 2. 挂载成功后,在虚拟机右下角c ...

  7. The Proof of Fibonacci GCD

    \[\Large \text{The Proof of Fibonacci GCD}\] \[\text{By Sangber}\] \(\text{Fibonacci Sequence}\) \(\ ...

  8. 「Luogu2264」情书

    传送门 Luogu 解题思路 字符串模拟SB题,STL随便搞. 详情见代码. 细节注意事项 STL总得会吧. 参考代码 #include <algorithm> #include < ...

  9. LoadRunner监控Linux系统

    需要下载3个包:  地址链接:链接:https://pan.baidu.com/s/1lltAa6JnjJ7Mr88duixUSQ 密码:5yiw(1)rsh-0.17-14.i386.rpm (2) ...

  10. 51nod 1099:任务执行顺序 贪心

    1099 任务执行顺序 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  取消关注 有N个任务需要执行,第i个任务计算时占R[i]个空间,而后会释放一部分, ...