题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
class Solution:
def isValid(self , s: str) -> bool:
if len(s) == 0:
return True
elif len(s) % 2 == 1:
return False
else:
stack = []
for c in s:
if c in ['(','{','[']:
stack.append(c)
elif len(stack) > 0:
last = stack.pop()
if c == ')' and last != '(':
return False
elif c == '}' and last != '{':
return False
elif c == ']' and last != '[':
return False
else: # 栈已空,c却是右半边
return False
else: # 循环正常走完
if len(stack) == 0:
return True
else: # 栈不空
return False
#刷题#
查看1道真题和解析
