1021. Delete to delete the outermost parentheses 1021. The outermost parentheses

1021. Delete to delete the outermost parentheses 1021. The outermost parentheses

1. topic
topic Link
Here Insert Picture Description

2. Analysis of the subject
title so long, meaning that the brackets of each set of removal of the outermost pair of parentheses, inside the parentheses save the new string.

3. Problem-solving ideas

  • method one:

    • Like this bracket matching, if written calculator function algorithm, we need to know to use the stack to match each pair of parentheses. Traversing brackets string S, the recording of each set of primitives with startIndex first "(" subscript, with the last recording endIndex primitive ")", then S.substring (startIndex + 1, endIndex) this primitive string that is removed after the outermost parentheses. When faced with a left parenthesis on the stack, when faced with a right parenthesis, took a left parenthesis pop the stack; when the stack is empty, it means to find a primitive.
  • Method Two:

    • The number of left bracket one pair of primitives will be equal to the right number of brackets, it is possible to traverse the string, then statistics about brackets the number of left, right, when the left! = When right, explain about the brackets are not equal, so the current traverse belong to primitive inside it can record this symbol; when left == right, explained that it had found a set of primitives; note that each time traversal, starting from the second symbol primitive (as the first symbol must be "("), this is also the reason for the initial value of 1 is left, the right = 0.

4. code implements (java)

package com.algorithm.leetcode.string;

import java.util.Stack;

/**
 * Created by 凌 on 2019/8/3.
 * 注释:
 */
public class RemoveOuterParentheses {
    public static void main(String[] args) {
//        String S = "(()())(())";
        String S = "()";
        String result = removeOuterParentheses(S);
        System.out.println(result);
        String result_1 = removeOuterParentheses_1(S);
        System.out.println(result_1);
    }
    public static String removeOuterParentheses(String S) {
        StringBuilder result = new StringBuilder();
        Stack<Character> stack = new Stack<>();
        int startIndex = 0;
        int endIndex = 0;
        for (int i = 0; i < S.length(); i++) {
            if (stack.isEmpty() && S.charAt(i) == '('){
                stack.push(S.charAt(i));
                startIndex = i;
            }else{
                if (S.charAt(i) == ')' && stack.peek() == '('){
                    if (stack.size() == 1){
                        endIndex = i;
                        stack.pop();
                        result.append(S.substring(startIndex+1, endIndex));
                    }else{
                        stack.pop();
                    }
                }else{
                    stack.push(S.charAt(i));
                }
            }
        }
        return result.toString();
    }
    public static String removeOuterParentheses_1(String S) {
        StringBuilder sb = new StringBuilder();
        int left = 1;
        int right = 0;
        char[] chs = S.toCharArray();
        for (int i = 1; i < chs.length; i++) {
            if (chs[i] == '('){
                left++;
            }else{
                right++;
            }
            if (left == right){
                left = 1;
                right = 0;
                i++;
            }else{
                sb.append(chs[i]);
            }
        }
        return sb.toString();
    }
}

Published 151 original articles · won praise 104 · views 270 000 +

Guess you like

Origin blog.csdn.net/qq_35923749/article/details/98393602