猿辅导7月31日算法题第一道(箱子计数),python版
def jiejue(str_):
if str_=="":
return 0
if str_=="[]":
return 1
num_all = 0
while len(str_)>2:
if str_[:2]=="[]" and str_[2]!= '[' and ']':
num_all += int(str_[2])*jiejue(str_[:2])
str_ = str_[3:]
elif str_[:2]=="[]" and str_[2]== '[':
num_all += jiejue(str_[:2])
str_ = str_[2:]
else:
stack_ = ['[']
i = 1
while True:
if str_[i]=='[':
stack_.append('[')
elif str_[i]==']':
stack_.pop()
if len(stack_)==0:
break
i += 1
if i+1<len(str_) and str_[i+1]!= '[' and ']':
num_all += int(str_[i+1])*(jiejue(str_[1:i])+1)
str_ = str_[i+2:]
else:
num_all += jiejue(str_[1:i])+1
str_ = str_[i+1:]
return num_all+jiejue(str_) #猿辅导2022秋招##猿辅导##笔经#

美的集团公司福利 747人发布