Python 3.x 缩短Python3中堆栈的使用时间
我正在处理一个编码问题 我已经写了一个解决方案Python 3.x 缩短Python3中堆栈的使用时间,python-3.x,algorithm,stack,Python 3.x,Algorithm,Stack,我正在处理一个编码问题 我已经写了一个解决方案 front = [] back = [] word = input() count = int(input()) for a in word: front.append(a) for i in range(count): command = input().split() if command[0] == "P": front.append(command[1]) elif command[0
front = []
back = []
word = input()
count = int(input())
for a in word:
front.append(a)
for i in range(count):
command = input().split()
if command[0] == "P":
front.append(command[1])
elif command[0] == "L":
if front:
back.append(front.pop())
elif command[0] == "D":
if back:
front.append(back.pop())
else:
if front:
front.pop()
while (back):
front.append(back.pop())
print(''.join(map(str, front)))
然而,当我提交时,我总是收到超时错误。
如何更改某些内容以在Python中更快地运行此代码?或者这是语言固有的问题?让我们想象一下,您有一百万个字符和两百万个命令
back
并将其附加到front
word
并记住当前光标所在的索引即可