题解 | 括号配对问题
括号配对问题
https://www.nowcoder.com/practice/57260c08eaa44feababd05b328b897d7
s=input()
stack=[]
for c in s:
if c=='(' or c=='[' or c=='{':
stack.append(c)
elif c==')':
if len(stack)>0 and stack[-1]=='(':
stack.pop()
else:
print("false")
exit()
elif c==']':
if len(stack)>0 and stack[-1]=='[':
stack.pop()
else:
print("false")
exit()
elif c=='}':
if len(stack)>0 and stack[-1]=='{':
stack.pop()
else:
print("false")
exit()
if len(stack) == 0:
print("true")
else:
print("false")
查看14道真题和解析
