| Title: |
Valid parentheses |
| Pass Rate: |
27.7% |
| Difficulty: |
Simple |
Given A string containing just the characters,,, ‘(‘ ‘)‘ , and ‘{‘ ‘}‘ ‘[‘ ‘]‘ , determine if the input string I S valid.
The brackets must close in the correct order, and is all valid but and is not "()" "()[]{}" "(]" "([)]" .
The topic is the traditional symbol matching problem with a stack to simulate on the line. Meet (, {, [All stacks, Encounters),},] perform a stack comparison, and if not () [] {} return an error and look directly at the code:
1 Public classSolution {2 Public BooleanIsValid (String s) {3 intlen=s.length ();4stack<character> stack =NewStack<character>(); 5 for(inti=0;i<len;i++){6 Chartmp=S.charat (i);7 if(tmp== ' (' | | | tmp== ' {' | | | tmp== ' ['){8 Stack.push (TMP);9 }Ten if(tmp== ') ' | | tmp== '} ' | | tmp== '] '){ One if(Stack.empty ())return false; A CharCtr=Stack.pop (); - if(tmp== ') ' &&ctr== ' (')Continue; - if(tmp== '] ' &&ctr== ' [')Continue; the if(tmp== '} ' &&ctr== ' {')Continue; - Else return false; - } - } + if(Stack.empty ())return true; - Else{ + return false; A } at } -}
Leetcode------Plus One