码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 1021 Remove Outermost Parentheses

时间:2019-05-26 19:40:10      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:substring   ==   nbsp   code   color   return   turn   int   toc   

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

标签:substring   ==   nbsp   code   color   return   turn   int   toc   

原文地址:https://www.cnblogs.com/stone94/p/10927031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!