Leetcode:valid Parentheses Problem Solving report

Source: Internet
Author: User

Valid parentheses

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

SOLUTION1:

Use stack to solve a simple problem. All the characters go in the stack sequentially

1. If you encounter a pair of brace stacks, the stack does not return false.

2. The stack is empty and can only be pressed into the left parenthesis

3. When the scan is complete, the stack should be empty, otherwise it will return false.

View Code

Solution 2:

It is easier to use a stack resolution. When you push the closing parenthesis, check that the opening parenthesis is not present and returns false if not, or an opening parenthesis is popped.

Finally, whether the stack is empty, not NULL for the number of parentheses asymmetric, also to return false;

1  Public classSolution {2      Public BooleanIsValid (String s) {3         if(s = =NULL) {4             return false;5         }6         7         intLen =s.length ();8         9         //bug 1:don ' t use s as the name of the stack.Tenstack<character> STK =NewStack<character>(); One          A          for(inti = 0; i < Len; i++) { -             Charc =S.charat (i); -             Switch(c) { the                  Case‘(‘: -                  Case‘[‘: -                  Case‘{‘: - Stk.push (c); +                      Break; -                  Case‘)‘: +                     if(!stk.isempty () && stk.peek () = = ' (') { A Stk.pop (); at}Else { -                         return false; -                     } -                      Break; -                  Case‘}‘: -                     if(!stk.isempty () && stk.peek () = = ' {') { in Stk.pop (); -}Else { to                         return false; +                     } -                      Break; the                  Case‘]‘: *                     if(!stk.isempty () && stk.peek () = = ' [') { $ Stk.pop ();Panax Notoginseng}Else { -                         return false; the                     } +                      Break;  A             } the         } +          -         returnstk.isempty (); $     } $}
View Code

Https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/stack/IsValid.java

Leetcode:valid Parentheses Problem Solving report

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.