# [Leedcode 20] Valid parentheses

Source: Internet
Author: User

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 `"()"` `"()[]{}"` `"(]"` `"([)]"` .

` Public classSolution { Public BooleanIsValid (String s) {//Solving : Need auxiliary stack to judge, time complexity is O (n), space complexity is O (n)//when the opening parenthesis is encountered, the right parenthesis needs to be judged: 1. Whether the stack is empty, 2. Whether the top of the stack matches this symbol. Returns False if there is a condition mismatch//Note the following details://1. Need to determine the stack when the stack empty//2. Final return results need to determine whether the stack is emptyStack<character> stack=NewStack<character>();  for(intI=0;i<s.length (); i++){            Chartemp=S.charat (i); if(temp== ' (' | | | temp== ' {' | | | temp== ' [') Stack.push (temp); Else{                if(temp== ') '){                    if(!stack.empty () && (Char) Stack.peek () = = ' (') Stack.pop (); Else                        return false; }                if(temp== ') '){                    if(!stack.empty () && (Char) Stack.peek () = = ' [') Stack.pop (); Else                        return false; }                if(temp== '} '){                    if(!stack.empty () && (Char) Stack.peek () = = ' {') Stack.pop (); Else                        return false; }            }        }        returnStack.empty (); }}`

[Leedcode 20] Valid parentheses

Related Keywords:

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.