[LeetCode] 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 = 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

题目大意是原字符串是有括号组成的,有单个括号,也可能有嵌套括号。将原字符串中的所有最外层括号去掉。比如"(())"会变成"()","(())(())"会变成"()()","()"会变成""。

因为要去掉最外层括号,所以将除最外层以外的"(",以及其配对的")"放入结果字符串中。使用flag标记")"是否是最外层括号的后半部分。

方法一:

我一开始想到了使用堆栈来存储"(",碰到")"时就将除最外层以外的括号都弹出到结果字符串中,然后使用一个flag标记遇到的")"是否是最外层括号的后半部分,如果是,就直接弹出而不放入结果字符串中。

代码如下:

class Solution {
public:
    string removeOuterParentheses(string S) {
        string res = "";
        stack<char> s;
        int flag = 0;
        for (int i = 0; i < S.size(); ++i) {
            if (S[i] == '(') {
                s.push(S[i]);
            }
            else if (S[i] == ')') {
                if (s.size() <= 1 && flag==0) {
                    s.pop();
                    continue;
                }
                while (s.size() > 1) {
                    res += s.top();
                    s.pop();
                    flag++;
                }
                res += S[i];
                flag--;
            }
        }
        return res;
    }
};

方法二:

但是使用flag的话其实根本没必要先将"("存入堆栈当中,只要不将最外层括号放入结果字符串中,遇到最外层")"时返回循环就好了。

代码如下:

class Solution {
public:
    string removeOuterParentheses(string S) {
        string res = "";
        int flag = 0;
        for (int i = 0; i < S.size(); ++i) {
            if (flag==0) {
                flag++;
                continue;
            }
            else if (S[i] == '(') {
                res += S[i];
                flag++;
            }
            else if (S[i] == ')') {
                if (flag > 1) {
                    res += S[i];
                }
                flag--;
            }
        }
        return res;
    }
};
原文地址:https://www.cnblogs.com/cff2121/p/11231886.html