题目如下:

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)

最新文章

  1. spring-cloud-event-sourcing-example-master 运行效果及说明
  2. HTML语言学习笔记(会更新)
  3. Python之列表、字符串、元组和字典的基本用法
  4. Xamarin开发Android笔记:TextView行间距设定
  5. .NET通过async/await实现并行
  6. jQuery_效果(滑动)
  7. 如何避免MVC Model First 设计时添加的DataAnnotation被覆盖掉
  8. Window Pains
  9. HTTP 和 Socket 的区别
  10. smarty 常用参数
  11. Lucence.Net学习+盘古分词
  12. linux命令 收集
  13. vtk基础编程(2)-读取数据文件中的坐标点
  14. 【JVM命令系列】jstack
  15. Bate冲刺 第四天
  16. P3957 跳房子
  17. Apache中httpd.conf文件的详解
  18. js 对象与数组相互转化的快捷方法 Object.keys()、Object.values()、Object.entries()
  19. GopherChina 2018
  20. 关于Django部分

热门文章

  1. Google Home Mini配置失败解决办法
  2. TCP/IP笔记——TCP特点、首部格式、滑动窗口
  3. python 并发编程 多线程 多线程实现并发的套接字通信
  4. centos6/7如何通过命令挑选出IP地址
  5. Python 正则匹配网页内的IP地址及端口号
  6. [转帖]目标管理的S.M.A.R.T.理念
  7. 用Python制作动态二维码
  8. 已知两个int变量a、b,定义4个方法分别对变量a、b进行加减乘除运算,并测试结果。
  9. UIWebView与JS的交互
  10. Adobe cc2019全家桶(免破解直接安装版)