4 --- algorithm interview stacks, queues, priority queues

The basic use of a stack

Example. 1: LeetCode 20 is. While this question is a simple topic but it is a classic problem, because around here to explain more here given code directly:

class Solution { 

    public  Boolean isValid (String S) { 
        Stack <Character> Stack = new new Stack <> ();
         for ( int I = 0; I <s.length (); I ++ ) {
             char C = s.charAt (I );
             iF (C == '{' || C == '[' || C == '(' ) { 
                stack.push (C); 
            } the else {
                 // that a solution will pop up when the stack is empty given situation, and can examine a mismatch 
                IF (stack.isEmpty ())
                     return  to false ;
                 char POP =stack.pop ();
                 // this is determined using the mismatch is more suitable 
                IF (C == '}' POP = &&! '{' ) {
                     return  to false ; 
                } the else  IF (C == ']' && POP = '! [ ' ) {
                     return  to false ; 
                } the else  IF (C ==') '= POP &&!' ( ' ) {
                     return  to false ; 
                } 
            } 
        } 
        return stack.isEmpty (); 
    } 
}

 Similarly topic there: LeetCode 150,71.

Example 2: recursive stack LeetCode 341. This question is used as the class exercises, where you can do it then analyze as examples.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0

 

Guess you like

Origin www.cnblogs.com/youngao/p/11532417.html
Recommended