20. Valid Parentheses [E] effective brackets

topic

Given a string containing just the characters '(',')','[',']','{' and ‘}’,determine if the input string is valid.
An input string is valid if:

  • 1.Open brackets must be closed by the same type of brackets.
  • 2.Open brackets must be closed in the correct order.
    Note that an empty string is also considered valid.

Example 1:
 Input:"()"
 Output:true
Example 2:
 Input:"()[]{}"
 Output:true
Example 3:
 Input:"(]"
 Output:false
Example 4:
 Input:"([)]"
 Output:false
Example 5:
 Input:"{[]}"
 Output:true


Thinking

Answers on leetcode is this:
first consider the simplest case: only one parenthesis


Figure 1: only one parenthesis

Define a counter left, if we encounter an open parenthesis, the counter left plus 1; if you encounter a closed parenthesis, then there are two cases, one of the current left = 0, indicating that no matching opening parenthesis, the expression invalid; the second is left> 0, indicating that there is no matching open bracket, left--.
But the reality is more than one parenthesis, but we found that in order to ensure that each sub-expression expressions are also effective, each with a closing parenthesis always left open parenthesis is the closest match, shown in Figure 2.

FIG 2: a plurality of brackets

This time is necessary to use the stack. When referring to a recursive structure problem, because we do not know the overall structure, can not solve the problem from the inside out, in which case the stack can help us recursively solve problems, to solve the problem from the outside to the mainland.
So problem-solving ideas this question is as follows:

  • 1. Initialize a stack, and initializes a mapping table (to handle different types of brackets);
  • 2. loop handle all brackets
    • If it is open parenthesis, then push into the stack can be;
    • If a closing parenthesis, then you need to check the elements of the stack, if the current top of the stack open bracket close bracket match (by looking to establish a mapping table to match), then pop the stack; if you do not match, indicating that expression is invalid;
  • 3. When finished with all brackets, if there are elements in the stack, indicating that expression is invalid.
    In a Diminshing wanted to play music only when opening parenthesis and closing parenthesis in order to eliminate the matching pair of parentheses.

Tips

Stack

A linear storage table. It has the following two characteristics:

  • Stack data in LIFO (last-in, first-out , LIFO) manner into the stack and the stack.
  • Data can only be operated from the top of the stack (top).

We can imagine it as a few dishes piled on the spring scene.


Figure 4: a schematic view of the structure of the stack

Push (push)

Figure 5: schematic Drawing

Pop (pop)

Figure 6: a schematic view of the stack


C++

 bool isValid(string s) {
        map<char,char> table={ {')', '('}, {']', '['}, {'}', '{'} };
        
        if( s.size()== 0 )
            return true;
        
        stack<char> cStack;
        for(int i = 0;i<s.size(); i++){
            if(s[i] == '(' || s[i] == '[' || s[i] == '{')
                cStack.push(s[i]);
            else if(s[i] == ')' || s[i] == ']' || s[i] == '}'){
                if(cStack.empty())
                    return false;
                char popChar = cStack.top();
                if(popChar == table[s[i]]){
                    cStack.pop();
                    continue;
                }
                else
                    return false;        
            }
          
        }
        if(cStack.empty())
            return true;
        return false;
    }

Python

Guess you like

Origin www.cnblogs.com/Jessey-Ge/p/10993511.html