leetcode--20. Valid Parentheses

来源:互联网 发布:unity3d透明材质 编辑:程序博客网 时间:2024/06/01 22:27

题目:20. Valid Parentheses

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

括号匹配。

python:

class Solution(object):    def isValid(self, s):        """        :type s: str        :rtype: bool        """        stack=["#"]        for i in range(len(s)):            if s[i] in ["[", "{", "("]:                stack.append(s[i])            else:                if s[i]==")":                    if stack[-1]!="(":                        return False                    else:                        stack.pop()                elif s[i]=="}":                    if stack[-1]!="{":                        return False                    else:                        stack.pop()                elif s[i]=="]":                    if stack[-1]!="[":                        return False                    else:                        stack.pop()        if len(stack)!=1:            return False        return True


原创粉丝点击