1021. 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 = P1 + P2 + … + Pk, where Pi 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: s = "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".

Example 2:

Input: s = "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".

Example 3:

Input: s = "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".

Constraints:

超级优雅的解法,不用stack

class Solution {public String removeOuterParentheses(String s) {StringBuilder sb = new StringBuilder();int open = 0;for (char c: s.toCharArray()) {if (c == '(' && open++ > 0) sb.append(c);if (c == ')' && open-- > 1) sb.append(c);}return sb.toString();}
}

算法:移除最外层的括号1021. Remove Outermost Parentheses相关推荐

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

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

  2. LeetCode 1021:Remove Outermost Parentheses

    C语言 char * removeOuterParentheses(char * S){int len = strlen(S);int j = 0;int sum = 0;for(int i = 0; ...

  3. 2019年4月8日 1021. Remove Outermost Parentheses

    class Solution(object):def removeOuterParentheses(self, S):""":type S: str:rtype: str ...

  4. 1021.删除最外层的括号

    有效括号字符串为空 ("")."(" + A + ")" 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接.例如 ...

  5. R语言dplyr包移除dataframe数据列实战(Remove Columns)

    R语言dplyr包移除dataframe数据列实战(Remove Columns) 目录 R语言dplyr包移除dataframe数据列实战(Remove Columns) #导入包和仿真数据

  6. R语言dplyr包移除dataframe数据行实战(Remove Rows):按照条件移除数据行、按照索引移除数据行、删除包含NA值的所有数据行、删除具体列包含NA值的数据行

    R语言dplyr包移除dataframe数据行实战(Remove Rows):按照条件移除数据行.按照索引移除数据行.删除包含NA值的所有数据行.删除具体列包含NA值的数据行 目录

  7. Bootstrap 3 移除输入框聚焦线 How to remove border (outline) around text/input boxes?

    一般可以用: textarea:focus, input:focus{outline: 0; } 或者 *:focus {outline: 0; } 来源:http://stackoverflow.c ...

  8. leetcode题解—1021、删除最外层的括号

    有效括号字符串为空 ("")."(" + A + ")" 或 A + B,其中 A 和 B 都是有效的括号字符串,+ 代表字符串的连接.例如 ...

  9. LeetCode 1021. 删除最外层的括号(栈)

    文章目录 1. 题目 2. 解题 1. 题目 题目链接 示例 1:输入:"(()())(())" 输出:"()()()" 解释: 输入字符串为 "(( ...

  10. leetcode - 1021. 删除最外层的括号

    有效括号字符串为空 ("")("")("")."("+A+")""(" + A ...

最新文章

  1. H5前端性能测试快速入门
  2. 【视频】React redux toolkit创建状态切片
  3. 大数据学习系列----大数据项目的思考
  4. Bootstrap3 表单帮助文本
  5. 使用多线程完成Socket
  6. 「leetcode」538.把二叉搜索树转换为累加树【递归】【迭代】详解
  7. 玄奥八字V5.6 爆破实例2
  8. 7 位 CEO 创业自述:你熬过人生中最黑暗时刻的经历,是你成为强者的必然选择...
  9. WebDAV之葫芦儿·派盘+Solid Explorer = 全能 Android 文件管理器
  10. Docker私有化部署RSSHub全记录
  11. 读《消费者行为心理学》-笔记
  12. oracle rac 成本及架构,Oracle Extended RAC 架构实战
  13. Elasticsearch中间隔查询slop原理
  14. CMakeLists.txt文件常见编译错误
  15. 2021Java校招笔试题答案及评分标准
  16. 攻防对抗形势下代码重用技术的演进
  17. PreTranslateMessage详解
  18. Tennessee Eastman(TE过程)简介
  19. 学习视频剪辑整理和所感
  20. Scrapy爬取网页并保存到数据库中

热门文章

  1. C#使用NPIO读写excle,并生成多个表单
  2. 在GridView中,设置字符超出单元格部分的内容用“...”表示
  3. Laravel User Agent 轻松识别客户端(微信)信息(2019版)
  4. RestTemplate 配置http连接池
  5. 【权值分块】bzoj1588 [HNOI2002]营业额统计
  6. Windows Phone 项目实战之我的微盘
  7. 云计算计算机二级,2021计算机二级office用的是哪个版本
  8. linux mysql 密码策略_linux密码策略
  9. ThinkPHP如何URL如何去掉index.php(重写模式)
  10. mysql post 注入工具类_【Mysql sql inject】POST方法BASE64编码注入write-up