题解 | 括号配对问题
括号配对问题
https://www.nowcoder.com/practice/57260c08eaa44feababd05b328b897d7
s = list(input())
you = []
stack = []
for x in s:
if x=='(' or x==')'or x=='['or x==']':
you.append(x)
if len(you)%2==0 and you[0] != ']'and you[0]!=')':
for x in you:
if x == '(' or x == '[':
stack.append(x)
elif (x == ')' and stack[len(stack)-1] == '(') or (x == ']' and stack[len(stack)-1] == '['):
stack.pop()
else:
break
if len(stack)==0:
print("true")
else:
print("false")
else:
print("false")
遍历取数浪费了时间,但可以len(you)%2==0 and you[0] != ']'and you[0]!=')'可以节约判断遍历时间
美的集团公司福利 852人发布
