leetcode-20- effective brackets

problem:

 

Package com.example.demo; 

Import the java.util.HashMap;
 Import a java.util.Map;
 Import the java.util.Stack; 

public  class Test20 { 

    / ** 
     * effective brackets 
     * utilize stack data structure encountered {[(et left parenthesis on the stack, it will encounter a right parenthesis element stack removed, compared 
     * Subject to continue determining a 
     * / 
    public  Boolean isValid (String S) { 

        the Map <String, String> Map = new new the HashMap < > (); 
        map.put ( "(", ")" ); 
        map.put ( "{", "}" ); 
        map.put ( "[", "]" );

        Stack<String> stack = new new Stack <> (); 

        for ( int I = 0; I <s.length (); I ++ ) { 
            String Sub = s.substring (I, I +. 1 );
             IF ( "(, {, [," . the contains (Sub)) { 
                stack.push (Sub); 
            } the else {
                 // If the stack has no elements is changed to a fixed value, the fixed value not belonging to one of the brackets, when the comparator returns to false 
                String POP ? = stack.isEmpty () "#" : stack.pop ();
                 IF (! {sub.equals (map.get (POP)))
                     return  false ; 
                } 
            } 
        }
        // is determined according to the stack when the matching has been completed when the element is still 
        return stack.isEmpty (); 
    } 

    public  static  void main (String [] args) { 
        Test20 T = new new Test20 ();
         Boolean Valid = t.isValid ( " () () (]) {} [] " ); 
        System.out.println (Valid); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/nxzblogs/p/11276685.html