Java version Bracket matching detection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.