- 如果popv下一个弹出的数字刚好是辅助栈中的栈顶,那么直接弹出
- 如果下一个弹出的数字不是栈顶,那么把pushv中还没有入栈的数字压入辅助栈,直到把下一个需要弹出的数字压入栈顶为止
- 如果所有数字都压入栈后仍然没有找到下一个弹出的数字,那么该序列不可能是一个弹出序列
class Solution:
def IsPopOrder(self, pushV, popV):
# write code here
helper = []
for j in popV:
if helper and j == helper[-1]:
helper.pop()
elif j in pushV:
i = 0
while j != pushV[i] and i < len(pushV):
helper.append(pushV[i])
i += 1
pushV = pushV[i+1:]
else:
return False
return True
京公网安备 11010502036488号