You have a string s that consists of English letters, punctuation marks, whitespace characters, and brackets. It is guaranteed that the parentheses in s form a regular bracket sequence.

Your task is to reverse the strings contained in each pair of matching parentheses, starting from the innermost pair. The results string should not contain any parentheses.

Example

For string s = "a(bc)de", the output should be
reverseParentheses(s) = "acbde".

我的解答:

 我也是佩服我能这样写出来.....
def reverseParentheses(s):
li = []
for x in s:
li.append(x)
while '(' in li:
for i in li:
if i == ')':
for j in range(li.index(i)-1,0,-1):
if li[j] == '(':
y = li.index(i)
z = j
li.pop(li.index(i))
li.pop(j)
l = []
l3 = []
for p in range(z, y-1):
l.append(p)
l2 = sorted(l, reverse=True)
for m in l:
l3.append(li[l2[l.index(m)]])
li[z:y-1] = l3
break
continue
s = ''.join(li)
return s
else:
s = ''.join(li)
return s

膜拜大佬:

 def reverseParentheses(s):
for i in range(len(s)):
if s[i] == "(":
start = i
if s[i] == ")":
end = i
return reverseParentheses(s[:start] + s[start+1:end][::-1] + s[end+1:])
return s

Code Signal_练习题_reverseParentheses的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_Knapsack Light

    You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...

  3. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  4. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  5. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  6. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  7. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. Code Signal_练习题_absoluteValuesSumMinimization

    Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...

随机推荐

  1. 【xsy1120】 支援(assist) dp+卡常

    妙啊算错时间复杂度了 题目大意:给你一棵$n$个节点的二叉树,每个节点要么是叶子节点,要么拥有恰好两个儿子. 令$m$为叶子节点个数,你需要在这棵二叉树中选择$i$个叶子节点染色,叶节点染色需要一定的 ...

  2. POJ 1102

    #include<iostream>// cheng da cai zi 11.14 using namespace std; int main() { int i; int j; int ...

  3. 【数组】Jump Game

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. vue2.0实现底部导航切换效果

    使用vue2.0写移动端的时候,经常会写底部导航效果,点击切换路由效果,实现图片和文字颜色切换.vue2.0也提供了很多ul框架供我们实现效果,今天就用原生的实现一个底部导航切换,直接上代码: 效果图 ...

  5. java8 学习之路之lambda

    前言 目前我们知道java的版本已经发布到12了,之前的项目用的是JDK1.7,听说JDK1.8的改动相对来说大一些,因此抽空学学JDK1.8的一些新特性.本人也是通过阅读Java8实战这本书做一些小 ...

  6. freeSWITCH之安装

    freeSWITCH 安装 官网教程 https://freeswitch.org/confluence/display/FREESWITCH/FreeSWITCH+First+Steps Windo ...

  7. kibana-1-安装

    由于es5.2版本对head的插件支持不如2.4 安装挺麻烦, 于是选用kibana 关于5.2安装head插件可见这个博客 http://www.cnblogs.com/xing901022/p/6 ...

  8. 笔记六:python字符串运算与函数

    一:学习内容 字符串运算 字符串函数-strip() 字符串函数-大小写互换 字符串函数-字符串对齐 字符串函数-搜索 字符串函数-替换 字符串函数-split切割 字符串函数-连接join 字符串函 ...

  9. Struts ongl 集合伪属性

    首先了解下OGNL的概念: OGNL是Object-Graph Navigation Language的缩写,全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的 ...

  10. 开源项目Log4j

    一:Log4j入门简介学习 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务器.NT的事件记录器.UNIX ...