leetcode20. 有效的括号

有效的括号
Note:
---
python中列表就是栈,具有pop顶层元素的功能
class Solution:
    def isValid(self, s):
        """
        :type s: str
        :rtype: bool
        """
        left = {'(','[','{'}
        map = {')':'(',']':'[','}':'{'}
        stack = []
        for val in s:
            if val in left:
                stack.append(val)
            elif stack and map[val] == stack.pop(): # 判断是否空列表 if stack 而不是if satck is None
                continue
            else:
                return False
        if stack: return False # 遗漏情况,只有左括号:‘[’
        return True

你可能感兴趣的:(leetcode20. 有效的括号)