【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 ...
随机推荐
- Servlet对用户输入的数据进行读取
逻辑代码: package com.zyb.test; import java.io.IOException; import java.util.Enumeration; import javax.s ...
- Victor and String[Bestcoder #52 1004](回文树)
题目描述 Victor喜欢玩字符串.他认为一个字符串是迷人的,当且仅当字符串是回文的.Victor想玩n次.每次他都会做以下四种操作中的一种.操作1:在字符串的开头添加一个字符 c.操作2:在字符串的 ...
- 如何使用IDEA将maven项目打成war包
1.点击IDEA界面右边的maven project 2.双击package
- PHP 符号大全
注解符号: // 单行注解 /* */ 多行注解 引号的使用 ’ ’ 单引号,没有任何意义,不经任何处理直接拿过来; " "双引号,PHP动态处理然后输出,一般 ...
- Link Analysis_1_Basic Elements
1. Edge Attributes 1.1 Methods of category 1.1.1 Basic three categories in terms of number of layers ...
- [Verilog] indexed part-select +:
That syntax is called an indexed part-select. The first term is the bit offset and the second term ...
- JavaWeb开发:从购买服务器到简单demo运行
写这篇文章的目的: 一个是为了记录实施过程,方便自己日后查阅: 另一个是给项目组成员提供一个参考,方便他们以后搭建自己的项目环境: 当然若能帮助到更多的朋友,那就再好不过了:D 需要注意: 我本身也是 ...
- 使用Spring JMS轻松实现异步消息传递
异步进程通信是面向服务架构(SOA)一个重要的组成部分,因为企业里很多系统通信,特别是与外部组织间的通信,实质上都是异步的.Java消息服务(JMS)是用于编写使用异步消息传递的JEE应用程序的API ...
- Day2:接着思考和可能的策划
今天早上闹钟还没响呢,老婆就把我叫醒了~说有规律宫缩了! 7点到8点记录了一个小时,宫缩差不多5~6分钟一次! 赶紧收拾东西上医院!正好今天是40周的产检,今天还是预产期! 这孩子终于肯出来了! 结果 ...
- PyCharm无法找到已安装的Python类库的解决方法
一.问题描述 软件系统:Windows10.JetBrains PyCharm Edu 2018.1.1 x64 在命令行cmd中安装python类库包Numpy.Matplotlib.Pandas. ...