【leetcode】1190. Reverse Substrings Between Each Pair of Parentheses
题目如下:
Given a string
sthat consists of lower case English letters and brackets.Reverse the strings in each pair of matching parentheses, starting from the innermost one.
Your result should not contain any bracket.
Example 1:
Input: s = "(abcd)"
Output: "dcba"Example 2:
Input: s = "(u(love)i)"
Output: "iloveu"Example 3:
Input: s = "(ed(et(oc))el)"
Output: "leetcode"Example 4:
Input: s = "a(bcdefghijkl(mno)p)q"
Output: "apmnolkjihgfedcbq"Constraints:
0 <= s.length <= 2000sonly contains lower case English characters and parentheses.- It's guaranteed that all parentheses are balanced.
解题思路:本题和leetcode之前出现过的四则运算的题目类似。从头开始遍历s,不是'('的字符直接入栈,如果遇到')',找出栈中最靠近栈顶的'(',逆置从'('到栈顶的所有元素,同时删除'(',直到s遍历完成为止。
代码如下:
class Solution(object):
def reverseParentheses(self, s):
"""
:type s: str
:rtype: str
"""
left_inx = []
stack = []
for i in s:
if i == '(':
stack.append(i)
left_inx.append(len(stack)-1)
elif i == ')':
inx = left_inx.pop(-1)
sub = stack[inx + 1:]
sub.reverse()
stack = stack[:inx] + sub
else:
stack.append(i)
return ''.join(stack)
【leetcode】1190. Reverse Substrings Between Each Pair of Parentheses的更多相关文章
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【LeetCode】151. Reverse Words in a String
Difficulty: Medium More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...
- 【LeetCode】#7 Reverse Integer
[Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
随机推荐
- CTF—攻防练习之HTTP—PUT上传漏洞
主机:192.168.32.152 靶机:192.168.32.159 中间件PUT漏洞 包括apache,tomcat,IIS等中间件设置支持的HTTP方法(get,post,head,delete ...
- python基础-python函数参数为print语句时的输出
函数参数输入print语句,调用函数时都会执行print语句,实例: def outer(func): def inner(): print("我是内层函数!") return i ...
- MYSQL5.5 linux安装
1.常规的编译安装MYSQL 此种方法使用所有Mysql5.0 - 5.1 系列产品 比较常规的编译方式 2. 采用cmake 方式编译安装Mysql 3.二进制安装方式 免编译安装MYSQL 4.如 ...
- mysql——触发器——示例
数据准备: ), d_id ), name ), age ), sex ), homeadd ) ); ,,,'nan','beijing'); ,,,'nv','hunan'); ,,,'nan', ...
- CountDownLatch与CyclicBarrier的对比
CountDownLatch: CountDownLatch通过计数器来实现,计数器表示线程的数量.每当一个线程执行结束后,计数器的值就会减1,并在await方法处阻塞.一旦计数器为0,所有阻塞的线程 ...
- Css设置最优先
input{ width: 220px !important; } css中 加上 !important 用一些前端框架,源文件修改不便时 可以这样用
- 如何使用 re模块的, spilt.
例: 这是一组 网卡的信息. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopb ...
- 【6.12校内test】T3 城市交通费
要不我先去写T2吧(逃 先把题目搞上来: [问题描述] 有 n 个城市,编号 1~n.其中 i 号城市的繁华度为 pi.省内有 m 条可以双向同行的高速 公路,编号 1~m.编号为 j 的高速公路连接 ...
- ZOJ 2836 Number Puzzle 题解
题面 lcm(x,y)=xy/gcd(x,y) lcm(x1,x2,···,xn)=lcm(lcm(x1,x2,···,xn-1),xn) #include <bits/stdc++.h> ...
- AT2294 Eternal Average
题目 题目给我们的这个东西可以转化为一棵\(k\)叉树,有\(n+m\)个叶子节点,其中\(m\)个权值为\(1\),\(n\)个权值为\(0\),每个非叶子节点的权值为其儿子的平均值,现在问你根节点 ...