题解 | 有效括号序列--栈
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param s string字符串
# @return bool布尔型
#
class Solution:
def isValid(self , s: str) -> bool:
# write code here
st=[]
flag=True
for i in s:
if i =='[' or i=='{' or i=='(':
st.append(i)
elif i==']'and len(st)>0: # 注意输入只有为“]”时
top=st.pop()
if top !='[':
flag=False
break
elif i=='}' and len(st)>0: # 注意输入只有为“}”时
top=st.pop()
if top !='{':
flag=False
break
elif i==')'and len(st)>0: # 注意输入只有为“)”时
top=st.pop()
if top !='(':
flag=False
break
else:
flag=False
break
if len(st)>0:
flag=False
return flag
#刷题#
查看9道真题和解析
腾讯成长空间 5881人发布

