Valid parentheses--Bracket Matching algorithm

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source:http://blog.csdn.net/pistolove/article/details/41450987

Through this article you can learn the following knowledge:

(1) The understanding of the stack in the data structure, especially the difference between the peek () method and the Pop () method in the Stack class.

(2) Understand the problem-solving ideas and improve the ability to think.


Given A string containing just the characters‘(‘,‘)‘,‘{‘,‘}‘,‘[‘and‘]‘, determine if the input string is valid.

the brackets must close in the correct order, " () "  and " () []{} "  are all valid But " (] "  and " ([)] "  are not.

Test instructions: Given a string containing three parentheses, determine if the parentheses in the string match correctly.

Problem Solving Ideas:

(1) by test instructions, the parentheses are interspersed between the characters, and we need to deal with parentheses, so the first step is how to separate the parentheses from the string. You can store the parentheses that appear by iterating through the strings .

(2) Since the parentheses are matched in pairs, the parentheses are stored in HashMap for subsequent query comparisons.

(3) We are using a stack to match the parentheses, we will (1) the parentheses are stored in the stack for comparison, the first parenthesis is stored in, when the second parenthesis enter the need to make a judgment. First, determine whether it is

The parenthesis to the left, which is judged by Leftlist.contains (), is pressed into the stack if it is not facing the left bracket, or if it is not facing the left bracket, you need to remove the element from the stack and the parentheses are compared, here

gets the current stack top element (without removing the element) using the Peep () method. Second, by using the map obtained in (2) to get the parentheses that match the current brackets, if the resulting parentheses match exactly , the existing elements are removed from the current stack,

The method for the top element of the stack is pop (), and if the resulting parentheses do not match, the match fails and returns false. Finally, and so on, until all the parentheses are matched to the end of the sentence .

PS: The above is certainly not the best way to solve the problem, but it is the result of my own thinking, although the code is very wordy, but the idea is relatively clear, I hope to help you, at the same time I hope you give advice, but also hope to have some exchanges with the great God.

The problem-solving code is as follows (PS: I technical comparison dishes, write code is also very vegetable, but OJ still can pass, everyone has a better idea hope to share, thank you):

public Boolean isValid (String s) {int len = S.length (); Boolean isValid = true; stack<character> stack = new stack<character> (); Character[] all = {' [', ' (', ' {', '] ', ') ', '} '; Character[] left = {'] ', ') ', '} '; list<character> leftlist = Arrays.aslist (left); list<character> alllist = arrays.aslist (All); Map<character, character> map = new Hashmap<character, character> (), Map.put (' [', '] '), Map.put (' (', ') '); Map.put (' {', '} '); Map.put ('} ', ' {'); Map.put (') ', ' ('); Map.put (') ', ' ['); linkedlist<character> linked = new linkedlist<character> (); for (int i = 0; i < len; i++) {if (Alllist.contai NS (S.charat (i))) {Linked.add (S.charat (i));}} if (Linked.size () ==0) return True;stack.push (Linked.get (0)), for (int i = 1; i < linked.size (); i++) {Character str = Li Nked.get (i); if (Leftlist.contains (str) && stack.size () > 0) {if (Map.get (str). Equals (Stack.peek ())) { Stack.pop ();} Else{isvalid = false;}} Else{stack.push (str);}} if (stack!=null && stack. Size () >0) {IsValid = false;}    return isvalid; }





Valid parentheses--Bracket Matching algorithm

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.