classSolution: defisValid(self, s: str) -> bool: stack = [] l, r = {'(', '[', '{'}, {')', ']', '}'} for c in s: if c in l: stack.append(c) elif c in r: # 栈不为空且与右括号类型一样 if c == ')'and stack and stack[-1] == '(': stack.pop() elif c == ']'and stack and stack[-1] == '[': stack.pop() elif c == '}'and stack and stack[-1] == '{': stack.pop() else: returnFalse returnlen(stack) == 0