【leetcode】1021. Remove Outermost Parentheses
题目如下:
A valid parentheses string is either empty
("")
,"(" + A + ")"
, orA + B
, whereA
andB
are valid parentheses strings, and+
represents string concatenation. For example,""
,"()"
,"(())()"
, and"(()(()))"
are all valid parentheses strings.A valid parentheses string
S
is primitive if it is nonempty, and there does not exist a way to split it intoS = A+B
, withA
andB
nonempty valid parentheses strings.Given a valid parentheses string
S
, consider its primitive decomposition:S = P_1 + P_2 + ... + P_k
, whereP_i
are primitive valid parentheses strings.Return
S
after removing the outermost parentheses of every primitive string in the primitive decomposition ofS
.Example 1:
Input: "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".Example 2:
Input: "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".Example 3:
Input: "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".Note:
S.length <= 10000
S[i]
is"("
or")"
S
is a valid parentheses string
解题思路:括号配对的题目在leetcode出现了很多次了,从左往右遍历数组,分别记录左括号和右括号出现的次数,当两者相等的时候,即为一组括号。
代码如下:
class Solution(object):
def removeOuterParentheses(self, S):
"""
:type S: str
:rtype: str
"""
left = 0
right = 0
res = ''
tmp = ''
for i in S:
tmp += i
if i == '(':
left += 1
else:
right += 1
if left == right:
res += tmp[1:-1]
tmp = ''
left = 0
right = 0
return res
【leetcode】1021. Remove Outermost Parentheses的更多相关文章
- 【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...
- 【Leetcode_easy】1021. Remove Outermost Parentheses
problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完
- 【leetcode】301. Remove Invalid Parentheses
题目如下: 解题思路:还是这点经验,对于需要输出整个结果集的题目,对性能要求都不会太高.括号问题的解法也很简单,从头开始遍历输入字符串并对左右括号进行计数,其中出现右括号数量大于左括号数量的情况,表示 ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【leetcode】1021. Best Sightseeing Pair
题目如下: Given an array A of positive integers, A[i]represents the value of the i-th sightseeing spot, ...
- Leetcode 1021. Remove Outermost Parentheses
括号匹配想到用栈来做: class Solution: def removeOuterParentheses(self, S: str) -> str: size=len(S) if size= ...
- LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号
https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
随机推荐
- sqlserver安装-1
原文地址: https://blog.csdn.net/qq_41432123/article/details/79053486 下载:(免费使用安装dev版) ed2k://|file|cn_sql ...
- windows下源码安装调试postgresql
环境:windows 10 postgresql版本:postgresql-9.6.5 使用工具:vs2017社区版 辅助工具:perl.diff.flex.bison 相关工具下载地址: perl下 ...
- 移动端rem布局屏幕适配插件(放js中便可使用)
/* doc:不用管:document对象 win:不用管:window对象 design:注意:设计稿的尺寸/物理像素*/ (function (doc, win,design) {// alert ...
- HTTrack Website Copier 扫描规则
+*.css +*.js -ad.doubleclick.net/* -mime:application/foobar+*.gif +*.jpg +*.jpeg +*.png +*.tif +*.bm ...
- Oracle Data Guard Protection Modes
Maximum Availability This protection mode provides the highest level of data protection that is poss ...
- PHP不使用内置函数intval(),实现字符串转整数
平时我们用PHP时,将字符串转化为整型时,一般都是使用 intval() 内置函数,那么如果我们自己写,怎么写一个呢? 此时我们可以利用 ASCII 码计算得整数的特性,因为每个字符都对应一个 ASC ...
- Embedding理解与代码实现
https://blog.csdn.net/songyunli1111/article/details/85100616
- Netty精进01
为什么要学习Netty? 目前基于Netty实现的一些优秀的开源框架:Dubbo.RocketMQ.Spark.Spring5.Flink.ElasticSearch.gRPC……这些还说明不了为什么 ...
- composer的自动加载机制(autoload)
composer的出现真是让人们眼前一亮,web开发从此变成了一件很『好玩』的事情,开发一个CMS就像在搭积木,从packagist中取出『积木』搭建在自己的代码中,一点一点搭建出一个属于自己的王国. ...
- 如何根据字典值的大小,对字典中的项排序---Python数据结构与算法相关问题与解决技巧
实际案例: 某班英语成绩以字典形式存储为: { 'LiLei' : 90, 'Jim' : 88, 'Lucy': 92 } 如何根据成绩高低,计算学生排名 -- 根据分数,进行排名,并且把排名信息添 ...