题目如下:

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)

【leetcode】1190. Reverse Substrings Between Each Pair of Parentheses的更多相关文章

  1. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  2. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  3. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  4. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  5. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  6. 【Leetcode】Evaluate Reverse Polish Notation JAVA

       一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  8. 【LeetCode】345. Reverse Vowels of a String 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用栈 双指针 日期 [LeetCode] 题目地址 ...

  9. 【LeetCode】541. Reverse String II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

随机推荐

  1. eclipse 引用静态库设置选项

    环境说明: 静态库文件项目:engine C++ 项目:server 在server项目中引用静态库的库文件libEngine.a 需要设置如图选项,才能引用静态库项目里的文件 主要设置: 1.inc ...

  2. Cocos2d-X网络编程(5) 使用Rapidjson解析数据

    Json基础及28种c++解析库性能对比 JSON 概念和特点:     JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)     JSON ...

  3. JSR303 校验扩展(分组、按顺序校验)

    1.在spring MVC 项目中使用JSR303 校验数据合法性,一般情况下使用方法为 (1)在接受数据的实体使用注解标添加校验规则 package com.hzsj.wechatdto; impo ...

  4. 刷机,twrp,安装xposed

    首先明白几个名词: recovery模式,类似于pc端的PE系统,每个手机都有自带的rec,但不好用,最好自己刷一个,现在市面最好用的是twrp fastboot模式,比recovery更底层,进入f ...

  5. CentOS 设置 yum源

    什么是 yum Yum(全称 Yellow Dog Updater)是一个在 Fedora 和 RedHat 以及 CentOS 中的 Shell 前端软件包管理器.基于 RPM 包管理,能够从指定的 ...

  6. jQuery全选功能

    $(document).ready(function(){ //为父按钮添加事件 $("#chk_all").click(function(){ var a=$("#ch ...

  7. 集合类Hash Set,LinkedHashSet,TreeSet

    集合(set)是一个用于存储和处理无重复元素的高效数据结构.映射表(map)类似于目录,提供了使用键值快速查询和获取值的功能. HashSet类是一个实现了Set接口的具体类,可以使用它的无参构造方法 ...

  8. mysql 的主从

    MySQL的Replication(英文为复制)是一个多MySQL数据库做主从同步的方案,特点是异步复制,广泛用在各种对MySQL有更高性能.更高可靠性要求的场合.与之对应的是另一个同步技术是MySQ ...

  9. php产品细节图多图上传示例代码 无刷新

    前台文件代码 upload.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  10. .htaccess A网站单页面301到B网站单页面

      .htaccess 301问题 A网站  a.com/a.html 301到 B网站 b.com/b.html   RewriteRule ^a.com/a.html$ http://www.b. ...