Lintcode Python Simple class topic 423. Valid parentheses sequence

Source: Internet
Author: User
Tags lintcode

Title Description:

Given a string that represents a sequence of parentheses, contains the following characters:,,, and ‘(‘, ‘)‘ ‘{‘ ‘}‘ ‘[‘ ‘]‘ , determine whether a valid parenthesis sequence.

Have you ever encountered this problem in a real interview? Yes

Sample Example

Parentheses must be "()" represented in order, "()[]{}" valid parentheses, but "([)]" not valid.

labelStack Google

Topic Analysis:

Loop string with left parenthesis in the stack.

In the closing parenthesis, take the element from the top of the stack and pair it to determine the pairing result.

Finally, determine whether the stack is not empty.

Source:

Class solution:    # @param {string} s A string    # @return {Boolean} whether the string is A valid parentheses    def Isvalidparentheses (self, s):        # Write Your code here        if s none:return False                x = [' [', ' (', ' {']        y = ["]" , ")", "}"]        z = ["()", "[]", "{}"]                res = [] for        I in S:            if I in x:                res.append (i) # into the stack            elif i in y:< c13/># If a closing parenthesis is received, but there is no opening parenthesis in res, the direct return is false                if res = = []:                    return false                else:                    temp = Res.pop ( -1) + i                    # The rest of the situation, out of the stack an opening parenthesis, judging whether the opening parenthesis + the right parenthesis is valid if                    temp not in Z:                        return False        # If all brace pairs are satisfied, but Res also has an opening parenthesis, return false        if Len (res)! = 0:            return False        return True

  

Lintcode Python Simple class topic 423. Valid parentheses sequence

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.