首页 > 其他分享 >leetcode-1021-easy

leetcode-1021-easy

时间:2023-03-11 13:44:36浏览次数:50  
标签:primitive parentheses 1021 string stack valid easy sb leetcode

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:

1 <= s.length <= 105
s[i] is either '(' or ')'.
s is a valid parentheses string.

思路一:字符自会出现两种情况,当出现 ( 字符时,只要检查一下前面出现的左右字符数目是否相等,就可以把前面的字符去除最外层的括号

    public String removeOuterParentheses(String s) {
        Deque<Character> stack = new ArrayDeque<>();

        char[] chars = s.toCharArray();
        StringBuilder sb = new StringBuilder();
        int left = 0;
        int right = 0;
        for (char c : chars) {
            if (c == '(' && left == right && left > 0) {
                sb.append(getInnerString(stack));
            } else {
                stack.push(c);
            }

            if (c == '(') {
                left++;
            } else {
                right++;
            }
        }

        sb.append(getInnerString(stack));

        return sb.toString();
    }

    private String getInnerString(Deque<Character> stack) {
        StringBuilder sb = new StringBuilder();

        stack.pop();
        while (stack.size() > 1) {
            sb.insert(0, stack.pop());
        }

        return sb.toString();
    }

标签:primitive,parentheses,1021,string,stack,valid,easy,sb,leetcode
From: https://www.cnblogs.com/iyiluo/p/17205778.html

相关文章

  • leetcode-1496-easy
    PathCrossingGivenastringpath,wherepath[i]='N','S','E'or'W',eachrepresentingmovingoneunitnorth,south,east,orwest,respectively.Youstart......
  • leetcode-1523-easy
    CountOddNumbersinanIntervalRangeGiventwonon-negativeintegerslowandhigh.Returnthecountofoddnumbersbetweenlowandhigh(inclusive).Example......
  • LeetCode|2457. 美丽整数的最小增量
    题目链接:2457.美丽整数的最小增量给你两个正整数n和target。如果某个整数每一位上的数字相加小于或等于target,则认为这个整数是一个美丽整数。找出并返回满......
  • 【LeetCode回溯算法#07】子集问题I+II,巩固解题模板并详解回溯算法中的去重问题
    子集力扣题目链接给你一个整数数组nums,数组中的元素互不相同。返回该数组所有可能的子集(幂集)。解集不能包含重复的子集。你可以按任意顺序返回解集。示例1:输......
  • eNSP的使用4  NAT网络地址转换 easyip
    静态转换:一对一绑定 (双向通信)路由器靠地址区分内部主机easy ip :多对一  (单向通信)路由器靠端口(临时端口)区分内部主机easy ip的配置:1.配置aclacl  2000rule perm......
  • easyexcel填坑-校验表头为空,或者不符合预期
    背景:easyexcelv3.1.5实体类已经使用注解@ExcelProperty标注需要导入的属性正文开始关闭忽略空行,防止第一行是空跳过校验 ignoreEmptyRow(false)。此处如果未关闭,第......
  • Leetcode24. 两两交换链表中的节点
    题目描述:给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 思路:使用虚拟头结点,这样会......
  • 【LeetCode回溯算法#06】复原IP地址详解(练习如何处理边界条件,判断IP合法性)
    复原IP地址力扣题目链接(opensnewwindow)给定一个只包含数字的字符串,复原它并返回所有可能的IP地址格式。有效的IP地址正好由四个整数(每个整数位于0到255之......
  • LeetCode306 累加数
    题目描述累加数是一个字符串,组成它的数字可以形成累加序列。一个有效的累加序列必须至少包含3个数。除了最开始的两个数以外,序列中的每个后续数字必须是它之前......
  • LeetCode|1410. HTML 实体解析器
    题目链接:1410.HTML实体解析器「HTML实体解析器」是一种特殊的解析器,它将HTML代码作为输入,并用字符本身替换掉所有这些特殊的字符实体。HTML里这些特殊字符和它......