【leetcode】1021. Remove Outermost Parentheses
题目如下:
A valid parentheses string is either empty
(""),"(" + A + ")", orA + B, whereAandBare valid parentheses strings, and+represents string concatenation. For example,"","()","(())()", and"(()(()))"are all valid parentheses strings.A valid parentheses string
Sis primitive if it is nonempty, and there does not exist a way to split it intoS = A+B, withAandBnonempty valid parentheses strings.Given a valid parentheses string
S, consider its primitive decomposition:S = P_1 + P_2 + ... + P_k, whereP_iare primitive valid parentheses strings.Return
Safter 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 <= 10000S[i]is"("or")"Sis 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 ...
随机推荐
- 高级Javascript代码
Javascript是一门很吊的语言,我可能学了假的JavaScript,哈哈,大家还有什么推荐的,补充送那啥邀请码. 本文秉承着:你看不懂是你SB,我写的代码就要牛逼. 1.单行写一个评级组件 &q ...
- mysql中 key 、primary key 、unique key 和 index 有什么不同
mysql中 key .primary key .unique key 和 index 有什么不同 key 是数据库的物理结构,它包含两层意义和作用, 一是约束(偏重于约束和规范数据库的结构完整性), ...
- openlayers筛选图层
很多时候需要筛选图层,例如选择交互(ol.interaction.Select). 图片来自官方:https://openlayers.org/en/v4.6.5/apidoc/ol.interact ...
- JAVA中short和short相加自动转化为int
精度小于int的数值运算的时候都回被自动转换为int后进行计算 所以,下面的代码会报编译错误 short s1 = 1;short s2 = 1;s1= (s1+s2); 必须改成: short s1 ...
- String中的CompareTo
在API源码中,String的compareTo其实就是一次比较两个字符串的ASCII码.如果两个字符串的ASCII相等则继续后续比较,否则直接返回两个ASCII的差值.如果两个字符串完全一样,则返回 ...
- 【PP系列】SAP 取消报工后修改日期
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[PP系列]SAP 取消报工后修改日期 前言 ...
- github编程类书籍
https://github.com/justjavac/free-programming-books-zh_CN
- 19.ThreadLocal方法解决代码不友好的问题
#ThreadLocal import threading #创建全局ThreadLocal loacl_school = threading.local() class Student(): def ...
- docker安装及基本命令
Ubuntu安装docker sudo apt-get install docker.io Centos安装docker # 更新系统软件包 yum -y upgrade # 官方下载地址 curl ...
- python之----------字符编码的原理
1.内存和硬盘都是用来存储的. CPU:速度快 硬盘:永久保存 2.文本编辑器存取文件的原理(nodepad++,pycharm,word) 打开编辑器就可以启动一个进程,是在内存中的,所以在编辑器编 ...