【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 ...
随机推荐
- git 几个commit点合并成一个commit点
在用git做版本控制器的时候,经常会遇到以下情况: 1.在做1个功能的时候,你自己觉得代码没问题了,就本地commit,然后提交代码,在gitlab上发起和并请求,老大看完之后,觉得你还有修改的地方, ...
- tomcat配置解决乱码问题
在服务器上,如果项目是Tomcat启动的,可以用以下方式的设置解决乱码问题: 方法1.在Tomcat的catalina.sh(或者catalina.bat)文件中,开头加入: set JAVA_OPT ...
- windown 使用python 自动切换网络
由于工作环境的特殊性,有时需要切换到内网工作,有时需要通过手机连接外网,想自动运行一个脚本就做完这事 首先在PC机上安装一个无线网卡,例如360wifi就可以. 除了python,还用到了DOS命令, ...
- leetcode 20. 有效的括号 (python)
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可被认为是 ...
- IsAjaxRequest
具体来说,IsAjaxRequest代码可以分解为以下功能: public static bool IsAjaxRequest(this HttpRequestBase request) { if ( ...
- 【ABAP系列】SAP ABAP基础-SQL的嵌套使用
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP基础-SQL的嵌 ...
- 【SD系列】SAP SD模块-创建供应商主数据BAPI
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP SD模块-创建供应商主数据B ...
- Windows Server2003 关闭 关机信息、开机ctrl+alt+del
取消CTRL+ALT+DEL win+R 或从"开始"打开"运行",输入gpedit.msc打开"组策略编辑器",依次展开"计算机 ...
- stl(set和map)
http://codeforces.com/gym/101911/problem/A Recently Monocarp got a job. His working day lasts exactl ...
- dp(最长升序列:二分查找时间优化nlogn)
We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, sele ...