If the definition of a pattern such as "{}[] ()" or "{[()]}" is valid, "[{]" or "(" mode is invalid, then we can use a stack or recursive descent method to implement.)
I'm going to do this once with a stack.
The idea of realization is.
When the start symbol is encountered (' [', ' {' or ' ('), we push it into the stack. When the end symbol is encountered, the element inside the pop will see if it matches, otherwise it returns false.
Public BooleanIsValid (String s) {Char[] cs =S.tochararray (); if(cs.length% 2! = 0) return false; Stack<Character> stack =NewStack<character>(); for(inti=0;i<cs.length;i++){ if(cs[i]== ' [' | | cs[i] = = ' (' | | cs[i] = = ' {') {Stack.push (cs[i]); }Else{ if(Stack.isempty ())return false; Switch(Stack.pop ()) { Case‘(‘: if(cs[i]!= ') ')return false; Break; Case‘[‘: if(cs[i]!= '])return false; Break; Case‘{‘: if(cs[i]!= '} ')return false; Break; } } } if(!stack.isempty ())return false; return true; }
Java uses a stack to judge valid parentheses