当前位置:网站首页>根据入栈顺序判断出栈顺序是否合理

根据入栈顺序判断出栈顺序是否合理

2022-07-05 04:00:00 诗与浪子

def is_pop_order(push, pop):
    """ 根据入栈顺序判断出栈顺序是否合理 :param push: 入栈顺序 :param pop: 出栈顺序 :return: """
    if len(push) == 0:
        return False

    stack = []
    j = 0
    for i in range(len(push)):
        stack.append(push[i])
        while j < len(pop) and stack and stack[-1] == pop[j]:
            stack.pop()
            j += 1

    if len(stack) == 0:
        return True
    else:
        return False


print(is_pop_order('1234', '4321'))
print(is_pop_order('1234', '1423'))
print(is_pop_order('1234', '2134'))
原网站

版权声明
本文为[诗与浪子]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44647926/article/details/125586688