Determining whether a valid brackets (c ++ description)

Straight to the point, assume that we have a long list by '{', '}', '[', ']', '(', ')' these brackets configured such as "{[}] [()" string of symbols like this, of course, our eyes can see that it is illegal, then how to use code to determine whether or not legal?

In fact, we can make use of the stack string of symbols to determine whether these illegal:

1. First, we need to build me one mapping symbols as follows:

. 1   Map < char , char > MP = {{ ' ) ' , ' ( ' }, { ' } ' , ' { ' }, { ' ] ' , ' [ ' }}; // mapping bracket

2. We traversed string

1. If the key mappings can find the current character str [i], we pop an element from the stack and get that element. Of course, if the stack is empty of empty words, we have for this character just assigned a value, for example, assigned to '@', we note that the variable is false top_element. Next we see whether the current character str [i] corresponding to the value of the map are the same from top_element, if different, it can be determined that the symbol string to be illegal.

2. Ruoguo the character key is not found str [i] mapping, it is first added to the stack.

3. The end of the cycle, if the stack is empty then the current is legal, if not empty, it indicates that the current string of illegal

4. The light may not be simply expressed in the language description, let's look illustration, we have assumed that the input "()" string, the specific process is as follows:

5. Well after reading map, then we have C ++ code to describe the process:

#include <the iostream> 
#include <Stack> 
#include < String > 
#include <Map>
 the using  namespace STD; 

class Solution 
{ 
public :
     BOOL isValid ( String S) 
    { 
        IF (S == "" ) // If an empty string also legitimate 
            return  to true ;
         IF (s.size () == 1 ) // only one character is certainly illegal 
            return  false ; 
        Stack < char > ST; 
        the Map< Char , char > MP = {{ ' ) ' , ' ( ' }, { ' } ' , ' { ' }, { ' ] ' , ' [ ' }}; // mapping brackets 
        for ( int I = 0 ; I <s.size (); I ++ ) 
        { 
            iF (mp.find (S [I]) = mp.end ())! // Find mp whether the symbol mapping 
            {
                 char top_ele = (st.size () = = 0 )? '# ' : St.top (); // get the top element, if it is empty then easily set a character 
                IF (top_ele =! ' # ' )                                // stack is not empty then the element pop 
                    st.pop ();
                 IF ( ! top_ele mp.find = (S [I]) -> SECOND) // If the value of this element and the element is popped mp correspondence map is not the same, the process directly returns to false 
                    return  to false ; 
            } 
            the else 
            { 
                st.push (S [ I]); // pushed onto the stack 
            } 
        } 
        return st.size () == 0 ; // if the stack is empty indicates that valid 
    } 
};

6. Note that the code directly in the map list is initialized, so this code must only be run on the next version of the 11 c ++.

Well, this is more than the entire contents of the share, if wrong treatise also hope we see you next time.

 

Guess you like

Origin www.cnblogs.com/maoqifansBlog/p/12498130.html