#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @return bool布尔型
#
class Solution:
def isValid(self, s: str) -> bool:
# write code here
dict1={'(':')',
'[':']',
'{':'}'}
stack = []
pop1=[]
for i in s:
#print('i',i)
if i in dict1.keys():
#print('push1',i)
stack.append(i)
elif i in dict1.values():
pop1.append(i)
# print('pop1',i)
# print('stack-1',stack[-1])
if stack and dict1[stack[-1]] == i:
stack.pop(-1)
pop1.pop(-1)
# print(stack)
# print(pop1)
if stack:
return False
else:
if pop1:
return False
else:
return True
A=Solution()
input='([{])'
B=A.isValid(input)
print(B)
Python-判断括号是否配对
最新推荐文章于 2022-08-29 15:48:33 发布