猿辅导第一题,菜鸡代码,欢迎指正

# 带括号的字符串解压
c = 1
for i in range(c):
a = list('A11B')
b = []
stack = []
while len(a):
# print(b,stack)
x = a.pop(0)
if x!=')' and not x.isdigit():
if x=='(':
if len(stack) and stack[-1]!='(':
b += stack
stack.append(x)

elif x==')':
nu = int(a.pop(0))
while a[0].isdigit():
nu = nu + a.pop(0)
tem = stack.pop()
while stack[-1].isalpha():
tem += stack.pop()
stack.pop()
tem = tem*nu
# print('中间',tem,stack)
if len(stack)!=0:
stack += (tem)
else:
b += (tem)
else:
nu = x
while len(a) and a[0].isdigit():
nu = nu + a.pop(0)
nu = int(nu)
# print(nu)
tem = stack.pop()
while len(stack) and stack[-1].isalpha():
tem = stack.pop()
if len(stack):
stack += tem*nu
else:
b.append(tem*nu)
stack = []

if len(stack):
b = b + stack
b = [str(x) for x in b]
# print(''.join(b))
print(''.join(b))

#猿辅导##笔试题目#
全部评论
真的是,总是一下来才有比较正常的思路,笔试的时候想的办法总是蠢得要死,我写了三个方法才60%,而且没有考虑((这种情况,时间过了之后两分钟之内就想起来用栈就可以了,哎
点赞 回复 分享
发布于 2019-08-03 21:00

相关推荐

11-04 14:10
东南大学 Java
_可乐多加冰_:去市公司包卖卡的
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务