Make an empty stack, and read the characters to the end. If a closed symbol is read, the empty stack is wrong, and if it does not match, the stack tail character pops up. Otherwise the read-in is an open character and is pressed into the stack. Finally, if the stack is empty, returns true.
Please refer to the Mystack class for details.
1 Public classKuohaojiancha {2 Private Char[] item = {' (', ') ', ' [', '] ', ' {', '} '};3 PrivateMystack stack =NewMystack ();4 Private Char[] str;5 Private intCurrentnum;6 PublicKuohaojiancha (String str) {7 This. str =Str.tochararray ();8 }9 Ten Public Booleandisplay () { One for(inti = 0; i < str.length; i++){ ACurrentnum =CharNumber (Str[i]); - if(Misbystackisempty (Str[i])) - return false; the - if(currentnum% 2 = = 1) { - if(Currentnum-charnumber (Char) Stack.pop ())! = 1) - return false; + } - ElseStack.push (Str[i]); + } A return true; at } - - Private intCharNumber (Charc) { - inti = 0; - for(; i < 6; i++){ - if(c = =Item[i]) in Break; - } to returni; + } - the Private BooleanMisbystackisempty (Charc) {//If a closed symbol is read, the empty stack times the wrong * if(Stack.isempty ()) $ if(currentnum% 2 = = 1)Panax Notoginseng return true; - return false; the } + A}
Java version Bracket matching detection