import itertools
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
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
10-14
4665
12-22
993
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交