classSolution(object):defisValid(self, s):"""
:type s: str
:rtype: bool
"""
stack =[]for i in s:if i =='(':
stack.append(')')elif i =='[':
stack.append(']')elif i =='{':
stack.append('}')elifnot stack or i != stack[-1]:# not stack: 还没有遍历完字符串,栈就已经为空# i != stack[-1]: 类型不匹配returnFalseelse:
stack.pop()returnTrueifnot stack elseFalse