class Solution:
def isValid(self, s: str) -> bool:
dic = {'(':')', '[':']', '{':'}', '?':'?'}
# 问号的作用是解决边界空栈.pop报错
stack = ['?']
for i in s:
if i in dic:
stack.append(i)
elif dic[stack.pop()] != i :
return False
# 解决如果剩一个单左括号,如果直接return stack会返回T rue
return len(stack) == 1
栈-力扣20 有效括号
最新推荐文章于 2024-09-05 22:39:50 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)