Leetcode 20. Valid Parentheses

作者: Zentopia | 来源:发表于2018-03-14 23:03 被阅读10次

Python 3 实现:

源代码已上传 Github,持续更新。

"""
20. Valid Parentheses

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.
"""


class Solution:
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        stack = []
        dic = {'(': ')', '[': ']', '{': '}'}
        set = ['(', '[', '{']

        if len(s) % 2 != 0:
            return False

        for c in s:
            if c in set:
                stack.append(c)
            else:
                if not stack or dic[stack.pop()] != c:
                    return False
        return not stack

if __name__ == '__main__':

    solution = Solution()
    print(solution.isValid('(('))

相关文章

网友评论

    本文标题:Leetcode 20. Valid Parentheses

    本文链接:https://www.haomeiwen.com/subject/kgzqqftx.html