机试刷题_NC52 有效括号序列【python】
NC52 有效括号序列
from operator import truediv
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @return bool布尔型
#
class Solution:
def isValid(self , s: str) -> bool:
if not s:
return True
stack = []
for char in s:
if char=='(' or char=='[' or char=='{':
stack.append(char)
elif char==')':
if not stack or stack.pop()!='(':
return False
elif char==']':
if not stack or stack.pop()!='[':
return False
elif char=='}':
if not stack or stack.pop()!='{':
return False
if stack:
return False
return True