class Solution:
def isValid(self, s: str) -> bool:
# 定义一个空栈,用于存储左括号
stack = []
# 定义一个字典,用于存储左右括号的对应关系
dic = {')':'(',']':'[','}':'{'}
# 遍历字符串s中的每个字符
for i in s:
# 如果栈不为空,且当前字符在字典中
if stack and i in dic:
if stack[-1] == dic[i]:
stack.pop()
else:
return False
else:
stack.append(i)
# 如果栈为空,则返回True,否则返回False
return not stack
08-07
252
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-07
291
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-09
234
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)