第二种方法输入示例 2 的数据出错 zltzlt 发表于 2020-4-20 13:33
输入空字符串出错
空字符是不是输出‘ ’?
def f380(s):
if not s:return " "
res = ''
for i in range(len(s)):
if s.isdigit():
res += '+' + s + '*'
elif s == '[':
res += '('
elif s == ']':
res += ')'
else:
res += '+1*%s' %repr(s)
return eval(res) def f380(code):
while ("[" in code) or ("]" in code):
code = find_min_words(code)
return code
def find_min_words(code):
pos_left= #储存'['的位置
pos_right= #储存']'的位置
for i in range(len(code)):
if code == "[":
pos_left.append(i)
if code == "]":
pos_right.append(i)
#寻找最深的重复内容
pos_change_min=len(code)
pos_L=0
pos_R=0
for i in pos_left:
for j in pos_right:
if 0<(j-i)<pos_change_min:
pos_change_min=(j-i)
pos_L=i
pos_R=j
min_words = code
#记录重复次数
num = ''
num_start = pos_left
num_end = pos_L
dig_start=0
flag=0
for i in range(num_start,num_end):
if code.isdigit():
if flag == 0:
flag = 1
dig_start = i
num+=code
#解码,更改原字符串
res=code[:dig_start]+int(num)*min_words+code
return res
fan1993423 发表于 2020-4-19 22:12
输入 "32c]" 结果有误 塔利班 发表于 2020-4-19 21:22
973 ms Python3005 发表于 2020-4-20 01:26
输入 "32" 出错 sjtuszy 发表于 2020-4-20 01:45
516 ms kkk999de 发表于 2020-4-20 09:55
def f380(s):
a,b = s.find('['),s.find(']')
s = s[:a-1]+s*int(s)+s
输入 "3]" 超出时间限制 风魔孤行者 发表于 2020-4-20 11:04
989 ms zltzlt 发表于 2020-4-20 17:28
输入 "32c]" 结果有误
好吧,忽略掉了大写的情况,已改
import re
def fun380(s):
try:
d=re.search('\d+',s).group()
while len(d):
a,b=re.search('(\d+)\[(+)\]',s).groups()
s=s.replace(a+'['+b+']',int(a)*b)
except:
return s @zltzlt楼主已写完 见10楼 您测测速 zltzlt 发表于 2020-4-20 13:33
输入空字符串出错
输入空字符串的话难道不算输入不合法吗 {:10_256:}{:10_256:}{:10_256:}
def f380(s):
if not s:
return ''
while s.find('[') + 1:
after = s.find(']')
before = s[:after].rfind('[')
rptnum = rptstr = ''
n = 0
while 1:
n += 1
if '0' <= s <= '9':
rptnum = s + rptnum
else:
break
rptstr = s
s = s.replace(s, int(rptnum)*rptstr, 1)
return s fan1993423 发表于 2020-4-20 20:46
好吧,忽略掉了大写的情况,已改
输入以下数据超时:
'sdfjng56]]]]]]]' TJBEST 发表于 2020-4-19 22:33
递归函数做的我想应该不超时
1064 ms 斐波纳税 发表于 2020-4-20 23:52
1613 ms
页:
1
[2]