【leetcode】1190. Reverse Substrings Between Each Pair of Parentheses
2024-09-03 17:18:50
题目如下:
Given a string
s
that 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 <= 2000
s
only 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)
最新文章
- spring-cloud-event-sourcing-example-master 运行效果及说明
- HTML语言学习笔记(会更新)
- Python之列表、字符串、元组和字典的基本用法
- Xamarin开发Android笔记:TextView行间距设定
- .NET通过async/await实现并行
- jQuery_效果(滑动)
- 如何避免MVC Model First 设计时添加的DataAnnotation被覆盖掉
- Window Pains
- HTTP 和 Socket 的区别
- smarty 常用参数
- Lucence.Net学习+盘古分词
- linux命令 收集
- vtk基础编程(2)-读取数据文件中的坐标点
- 【JVM命令系列】jstack
- Bate冲刺 第四天
- P3957 跳房子
- Apache中httpd.conf文件的详解
- js 对象与数组相互转化的快捷方法 Object.keys()、Object.values()、Object.entries()
- GopherChina 2018
- 关于Django部分
热门文章
- Google Home Mini配置失败解决办法
- TCP/IP笔记——TCP特点、首部格式、滑动窗口
- python 并发编程 多线程 多线程实现并发的套接字通信
- centos6/7如何通过命令挑选出IP地址
- Python 正则匹配网页内的IP地址及端口号
- [转帖]目标管理的S.M.A.R.T.理念
- 用Python制作动态二维码
- 已知两个int变量a、b,定义4个方法分别对变量a、b进行加减乘除运算,并测试结果。
- UIWebView与JS的交互
- Adobe cc2019全家桶(免破解直接安装版)