作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/remove-outermost-parentheses/

题目描述

A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B 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 into S = A+B, with A and B nonempty valid parentheses strings.

Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.

Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.

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:

  1. S.length <= 10000
  2. S[i] is “(” or “)”
  3. S is a valid parentheses string

题目大意

找出字符串中每对括号匹配的结果去除最外层的()之后的结果之和。

解题方法

遍历

看到括号匹配,大家一般都想到用一个栈,其实不用栈也可以。

我们只需要一个变量count保存左括号数-右括号数即可。即遇到左括号则自增1,遇到右括号则自减1.当count为0的时候,说明在这一段中左括号和右括号相等,是个完美匹配的括号串了。

我们使用一个变量previ保存的是上一次括号完全匹配之后,下一个括号匹配开始位置。由于这个题需要让我们把最外层的括号去掉,所以,当count==0的时候,我们把结果增加的是[previ + 1, i),左闭右开区间。

Python代码如下:

class Solution(object):
def removeOuterParentheses(self, S):
"""
:type S: str
:rtype: str
"""
previ = 0
res = ""
count = 0
for i, s in enumerate(S):
if s == '(':
count += 1
else:
count -= 1
if count == 0:
res += S[previ + 1: i]
previ = i + 1
return res

日期

2019 年 4 月 7 日 —— 周赛bug了3次。。

【LeetCode】1021. Remove Outermost Parentheses 解题报告(Python)的更多相关文章

  1. Leetcode 1021. Remove Outermost Parentheses

    括号匹配想到用栈来做: class Solution: def removeOuterParentheses(self, S: str) -> str: size=len(S) if size= ...

  2. LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号

    https://leetcode-cn.com/problems/remove-outermost-parentheses/ Java Solution class Solution { public ...

  3. 【Leetcode_easy】1021. Remove Outermost Parentheses

    problem 1021. Remove Outermost Parentheses 参考 1. Leetcode_easy_1021. Remove Outermost Parentheses; 完

  4. 【leetcode】1021. Remove Outermost Parentheses

    题目如下: A valid parentheses string is either empty (""), "(" + A + ")", ...

  5. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  6. 1021. Remove Outermost Parentheses删除最外层的括号

    网址:https://leetcode.com/problems/remove-outermost-parentheses/ 使用栈的思想,选择合适的判断时机 class Solution { pub ...

  7. LeetCode 1021. 删除最外层的括号(Remove Outermost Parentheses)

    1021. 删除最外层的括号 1021. Remove Outermost Parentheses 题目描述 有效括号字符串为空 ("")."(" + A + ...

  8. LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)

    709. To Lower Case(Easy)# Implement function ToLowerCase() that has a string parameter str, and retu ...

  9. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

随机推荐

  1. ss 显示socket状态

    ss ===show socket用于显示socket状态 所有的TCP sockets 所有的UDP sockets 所有ssh/ftp/ttp/https持久连接 所有连接到Xserver的本地进 ...

  2. GWAS在农业上应用

    农业的组学技术应用虽然落后于人的研究,这是什么意义的问题,但有时农业基因组有自己无可比拟的优势,那就是材料.下面介绍GWAS应用. GWAS(Genome-wide association study ...

  3. C语言 序列反向互补函数

    1 static char *revers(char *s) 2 { 3 int len=strlen(s); 4 char *s2=(char *)malloc(sizeof(char)*(len+ ...

  4. mysql order by 多个字段排序实现组内排序

    总结:大组在前,小组在后,计量值再最后,即可实现组内排序:下边是参考别人的具体实例: 工作中需用到order by 后两个字段排序,但结果却产生了一个Bug,以此备录. [1]复现问题场景 为了说明问 ...

  5. Unity——Js和Unity互相调用

    Unity项目可以打包成WebGl,打包后的项目文件: Build中是打包后的Js代码: Index.html是web项目的入口,里面可以调整web的自适应,也可以拿去嵌套: TemplateData ...

  6. 学习java 7.11

     学习内容: 泛型定义格式:<类型> 优点:把运行时期的问题提前到编译期间:避免了强制类型转换   泛型方法:public class Fanxing { public <T> ...

  7. mybatis缓存+aop出现的问题

    在对某些特殊数据进行转换时,getOne方法后执行fieleInfoHandle进行转换,如果直接使用fixedTableData进行操作,没有后续的二次调用这样是没问题的,但是在后面当执行完upda ...

  8. Spring的事务传播机制(通俗易懂)

    概述 Spring的事务传播机制有7种,在枚举Propagation中有定义. 1.REQUIRED PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就 ...

  9. @NotBlank 注解不生效

    1. @NotBlank 注解是用来校验 String 类型的参数是否为空的 2. 使用方法 (1)Spring-boot 某一个版本之前 spring-boot-starter-web 中有包含 h ...

  10. Servlet+Jdbc+mysql实现登陆功能

    首先是新建一个servlet,servlet中有dopost和doget方法 一般的表格提交都是用post方法,故在dopost里面写入逻辑代码 下面是其逻辑代码Check.java protecte ...