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);
    }
}

 

posted @ 2019-05-26 18:51  stoneBlog  阅读(103)  评论(0编辑  收藏  举报