LeetCode 1021 Remove Outermost Parentheses

Posted stone94

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 1021 Remove Outermost Parentheses相关的知识,希望对你有一定的参考价值。

class Solution {
    public String removeOuterParentheses(String S) {
        int outer = 0;
        int inner = 0;
        char[] intput = S.toCharArray();
        char[] output = new char[intput.length];
        int i = 0;
        for (char c: intput) {
            if (outer != 0) {
                if (inner == 0) {
                    if (‘)‘ == c) {
                        outer += 1;
                    } else {
                        inner -= 1;
                        output[i] = c;
                        i += 1;
                    }
                } else {
                    if (‘(‘ == c) {
                        inner -= 1;
                        output[i] = c;
                        i += 1;
                    } else {
                        inner += 1;
                        output[i] = c;
                        i += 1;
                    }
                }
            } else {
                outer -= 1;
            }
        }
        return new String(output).substring(0,i);
    }
}

 

以上是关于LeetCode 1021 Remove Outermost Parentheses的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 1021 Remove Outermost Parentheses

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

LeetCode --- 1021. Remove Outermost Parentheses 解题报告

LeetCode --- 1021. Remove Outermost Parentheses 解题报告

LeetCode --- 1021. Remove Outermost Parentheses 解题报告

LeetCode 1021:Remove Outermost Parentheses