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) + ...
随机推荐
- 二,windows下安装memcached服务
window下安装memcached服务的流程如下: 1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached 2. 在终端(也即cmd命令界面)下输入 ‘c ...
- 00-python概述。
人生苦短,我用Python. -发展历史: - 1989年,由Guido van Rossum开始开发, - 1991年,发布第一个公开发行版,第一个Python编译器(同时也是解释器)诞生. - 2 ...
- 用 Python+nginx+django 打造在线家庭影院
用 Python+nginx+django 打造在线家庭影院 2018年11月29日 08:46:59 清如許 阅读数:1528 我喜欢看电影,尤其是好的电影,我会看上三四遍,仔细感受电影带给我的 ...
- day55 linux 基础以及系统优化
Linux系统基础优化及常用命令 Linux基础系统优化 引言没有,只有一张图. Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令 ...
- Linux 基础命令 持续更新中...
1.ls 显示当前文件/文件夹 显示文件大小: ls -lh 显示隐藏文件: ls -a 显示文件详细信息: ls -l (ll)2.pwd 显示当前所在路径 cat 显示当前文件下所有内容3.cd ...
- Linux CentOS7系统中ssh的用法
大家都知道,公司买上服务器,不可能实时在线操作虚拟机,也没有那个时间和精力登录到公司的云服务商官网进行操作,一来不安全,二来也效率不高. 如果是购买的虚拟主机,你可以使用ftp进行本地程序文件传输和从 ...
- ActiveRecord::Fixture::FormatError: ActiveRecord::Fixture::FormatError
环境:window 7+ruby2.33+rails5.0.. 该提示的意思是固件格式错误: 但是又没有提示是哪一行 非常蛋疼,我照成的原因居然是没有对齐,请看:(下面的activated_at没有和 ...
- [转] 用协议分析工具学习TCP/IP
一.前言 目前,网络的速度发展非常快,学习网络的人也越来越多,稍有网络常识的人都知道TCP/IP协议是网络的基础,是Internet的语言,可以说没有TCP/IP协议就没有互联网的今天.目前号称搞网的 ...
- node服务端搭建学习笔记
咳咳,终于迈出这一步了...这篇文章将是边学边写的真正笔记...用于mark下学习过程中的点滴~ 开篇先把我学习参考的文章来源给出,以表示对前人的尊敬: https://github.com/nswb ...
- springboot-5-整合jpa
######## ##springboot-parent.version: ## jdk 1.8 ## ####### 在整合jpa之前, 先说下mysql 步骤: 1), 在application. ...