【LeetCode】反转每对括号间的子串
【问题】给出一个字符串 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】反转每对括号间的子串的更多相关文章
- LeetCode:有效的括号【20】
LeetCode:有效的括号[20] 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. ...
- 每日一道 LeetCode (48):最长回文子串
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [LeetCode] 20. 有效的括号 (栈)
思路: 首先用字典将三对括号存储,遍历字符串中每个字符,遇到左括号就入栈:遇到右括号就开始判断:是否与栈弹出的顶字符相同. 如果到最后栈被清空,说明全部匹配上了,为真. class Solution( ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 28 Implement strStr() (实现找子串函数)
题目链接: https://leetcode.com/problems/implement-strstr/?tab=Description Problem : 实现找子串的操作:如果没有找到则返回 ...
- leetcode 最长有效括号
给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()&quo ...
- LeetCode Golang 5. 最长回文子串
5. 最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab&quo ...
随机推荐
- Navicat相关注册码
用户名和组织随便起. 注册码如下 --Navicat for SQL Server V10.0.10 NAVD-3CG2-6KRN-IEPM NAVL-NIGY-6MYY-XWQE NAVI-C3UU ...
- 网络协议-dubbo协议
Dubbo支持dubbo.rmi.hessian.http.webservice.thrift.redis等多种协议,但是Dubbo官网是推荐我们使用Dubbo协议的. 下面我们就针对Dubbo的每种 ...
- jwt 认证
目录 jwt 认证示意图 jwt 认证算法:签发与检验 drf 项目的 jwt 认证开发流程(重点) drf-jwt 框架基本使用 token 刷新机制(了解) jwt 认证示意图 jwt 优势 1 ...
- 可以看一下我学习linux的过程
学习Linux的最佳方法是将它用于日常工作. 阅读Linux书籍,观看Linux视频不仅仅是足够的. 学习Linux没有捷径可走. 你不可能在一夜之间在Linux中掌握. 这需要时间和持久性. 刚刚潜 ...
- zookeeper logs is missing zookeeper 日志丢失
ERROR [main:QuorumPeerMain@85] - Invalid config, exiting abnormally Invalid config, exiting abnormal ...
- matlab练习程序(概率路线图PRM)
PRM概率路线图全称 Probabilistic Roadmap,是一种路径规划算法,利用随机撒点的方式将空间抽样并将问题转为图搜索,利用A*或Dijkstra算法找到起始结束节点的最短路径. 可以想 ...
- 为什么Fun函数能够执行
#include<stdio.h> #include<windows.h> void Fun() { printf("Kali-Team\n"); } in ...
- Python 的 os 与 sys 模块
os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...
- Eclipse设置自动提示代码(不用alt+/了)
在preferences找到如图的相关位置.在输入框里把26个字母加进去,qwer...........
- loadrunner controller查看参数日志
1.在generator,我们用这个语句来输出参数:lr_output_message("目录为%s", lr_eval_string("{NewParam}" ...