8.23腾讯技术研究类和数据分析 笔试
第一题 括号匹配,用了两种方法,第三个样例一直不对,提交了之后也只过了10%的case,求解哪里出错了呢?
附上我的辣鸡代码,思路没变,用了两种写法
s=")([]]([(](])))([]()()]([][[)[()[)]([[(])][][[[([)]"
left_round=0
left_square=0
ans=0
for x in s:
if x=='(':
left_round+=1
elif x==')':
if left_round<1:
ans+=1
else:
left_round-=1
elif x=='[':
left_square+=1
else:
if left_square<1:
ans+=1
else:
left_square-=1
ans+=left_round+left_square
print(ans)
strs=")([]]([(](])))([]()()]([][[)[()[)]([[(])][][[[([)]"
strs=input()
flag=1
stack_round=[]
stack_square=[]
ans=0
for x in strs:
if x=="(":
stack_round.append(x)
elif x=="[":
stack_square.append(x)
elif x==")":
if len(stack_round)==0:
ans+=1
else:
stack_round.pop()
elif x=="]":
if len(stack_square)==0:
ans+=1
else:
stack_square.pop()
print(ans,len(stack_round),len(stack_square))
ans+=(len(stack_round)+len(stack_square))
print(ans) 
