foolish fly fox's blog

--Stay hungry, stay foolish.
--Forever young, forever weeping.



博客主页为:https://foolishflyfox.github.io/CsLearnNote/

20. Valid Parentheses

https://leetcode.com/problems/valid-parentheses/description/

Description

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

An input string is valid if:

Note that an empty string is also considered valid.

Example 1:
Input: "()"
Output: true

Example 2:
Input: "()[]{}"
Output: true

Example 3:
Input: "(]"
Output: false

Example 4:
Input: "([)]"
Output: false

Example 5:
Input: "{[]}"
Output: true

Solution

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        def addc_factory(c):
            def addc(a):
                a.append(c)
                return True
            return addc
        def popc_factory(c):
            pair_v = {']':'[', ')':'(', '}':'{'}
            def popc(a):
                if len(a)==0 or a[-1]!=pair_v[c]:
                    return False
                else:
                    a.pop()
                    return True
            return popc
        stack = []
        func_d = dict()
        for c in ['(', '{', '[']:
            func_d[c] = addc_factory(c)
        for c in [')', '}', ']']:
            func_d[c] = popc_factory(c)
        for c in s:
            if func_d[c](stack)==False:
                return False
        return len(stack)==0

一种更加简洁的实现:

class Solution(object):
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        c_dict = {'(':')', '[':']', '{':'}'}
        stack = []
        for c in s:
            if c in c_dict:
                stack.append(c)
            elif len(stack)==0 or c_dict[stack.pop()]!=c:
                return False
        return len(stack)==0