当前位置:网站首页>Possible stack order of stack order with length n

Possible stack order of stack order with length n

2022-07-05 04:01:00 Poetry and prodigal son

import itertools


def is_pop_order(push, pop):
    """  Judge whether the stack order is reasonable according to the stack order  :param push:  Stack order  :param pop:  Out of stack order  :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


if __name__ == '__main__':
    push = '123'
    sequences = list(itertools.permutations(push, 3))
    for sequence in sequences:
        pop = ''.join(sequence)
        if is_pop_order(push, pop):
            print(pop)

    # 1 2 3
    # 1 3 2
    # 2 1 3
    # 2 3 1
    # 3 1 2 x
    # 3 2 1
原网站

版权声明
本文为[Poetry and prodigal son]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050359502160.html