Code Signal_练习题_reverseParentheses
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 bereverseParentheses(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的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_Knapsack Light
You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- 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) + ...
随机推荐
- 实现一个算法,寻找字符串中出现次数最少的、并且首次出现位置最前的字符 如"cbaacfdeaebb",符合要求的是"f",因为他只出现了一次(次数最少)。并且比其他只出现一次的字符(如"d")首次出现的位置最靠前。
实现一个算法,寻找字符串中出现次数最少的.并且首次出现位置最前的字符如"cbaacfdeaebb",符合要求的是"f",因为他只出现了一次(次数最少).并且比其 ...
- 前端ajax传数据成功发送,但后端接收不到
前几天遇到这样的问题,找了好久,是在ajax contentType属性设置的问题. contentType默认是application/x-www-form-urlencoded 但是 ...
- zookeeper链接数导致kafka storm不能正常工作
报错信息: java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: org.apache ...
- java中的安全模型(沙箱机制)
java中的安全模型(沙箱机制) java安全沙箱(一)之ClassLoader双亲委派机制 java安全沙箱(二)之.class文件检验器 java安全沙箱(三)之内置于Java虚拟机(及语言)的安 ...
- iOS 关于布局问题的一些认识
///更新约束和布局 更新约束布局相关的API - (void)updateConstraintsIfNeeded 调用此方法,如果有标记为需要重新布局的约束,则立即进行重新布局,内部会调用upda ...
- js04
接着看一些js的基础,这里主要说一下js的对象. 1.对象: js中的所有事物都可以看作是对象:字符串.数值.数组.函数... 内建对象:String Date Array ...
- pycharm 使用jupyter notebook 报错:'_xsrf' argument missing from POST
出问题的关键点就在: 我用cmd启动的jupyter notebook,然后用pycham新建了一个jupyter notebook 结果 一直报错'_xsrf' argument missing f ...
- 解决linux安装软件依赖的曲线救国方案
相信大家在一台无法连接外网的linux上安装软件时,对于软件依赖的安装,都会特别头疼,因为软件依赖的安装,不论是其数量,还是安装的复杂度都比软件本身要高出一个维度! 今天就和大家分享一个,解决linu ...
- surgemq 添加ssl
surgemq添加ssl 1.MQTT协议 消息发布订阅功能的消息队列协议 报文组成: 固定报头(控制报文类型)+可变报头(协议名称.协议级别.连接标志.保持连接)+有效载荷(clentId+will ...
- 自定义针对Product Key处理的TextBox
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...