LeetCode [1021 Remove outermost brackets]

This question can be thought stack, S brackets only, we can think how can judge brackets are complete brackets,

Can then define a variable inlet (plus 1, feed) minus 1, 0 is determined, it is to enter the complete brackets, and then,

Using a new string receiving brackets, substring intermediate, remove the outermost bracket, this is also the use of

Receiving a string, note that the received character string needs to be emptied.

class Solution {
    public String removeOuterParentheses(String S) {
        int i;
        int f = 0;
        int a = 0;
        String s = "";
        String t = "";
        for(i = 0;i < S.length();i++)
        {
            if(S.charAt(i) == '(')
            {
                s = s + "(";
                f = f + 1;
                a++;
            }
            else
            {
                s = s + ")";
                f = f - 1;
                a++;
            }
            if(f == 0)
            {
                t = t + s.substring(1,a-1);
                s = "";
                a = 0;
            }
        }
        return t;
    }
}

 

Guess you like

Origin www.cnblogs.com/wzwi/p/10949208.html