当前位置:网站首页>长度为n的入栈顺序的可能出栈顺序
长度为n的入栈顺序的可能出栈顺序
2022-07-05 04:00:00 【诗与浪子】
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
边栏推荐
- An elegant program for Euclid‘s algorithm
- 特殊版:SpreadJS v15.1 VS SpreadJS v15.0
- MindFusion. Virtual Keyboard for WPF
- 一文带你了解BI的前世今身与企业数字化转型的关系
- [positioning in JS]
- [understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
- How to use jedis of redis
- [untitled]
- The architect started to write a HelloWorld
- 【web源码-代码审计方法】审计技巧及审计工具
猜你喜欢
Official announcement! The third cloud native programming challenge is officially launched!
error Couldn‘t find a package. JSON file in "your path“
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
NEW:Devart dotConnect ADO. NET
Clickhouse物化视图
How to use jedis of redis
Clickhouse同步mysql(基于物化引擎)
陇原战“疫“2021网络安全大赛 Web EasyJaba
Redis source code analysis: redis cluster
Laravel8 export excel file
随机推荐
DMX parameter exploration of grandma2 onpc 3.1.2.5
程序员的视力怎么样? | 每日趣闻
测试开发是什么?为什么现在那么多公司都要招聘测试开发?
面试汇总:这是一份全面&详细的Android面试指南
C # use awaiter
IronXL for .NET 2022.6
特殊版:SpreadJS v15.1 VS SpreadJS v15.0
请问一下我的请求是条件更新,但在buffer中就被拦截了,这种情况我只能每次去flush缓存么?
How does the applet solve the rendering layer network layer error?
[move pictures up, down, left and right through the keyboard in JS]
【做题打卡】集成每日5题分享(第三期)
Uni app common functions /api
[web Audit - source code disclosure] obtain source code methods and use tools
NEW:Devart dotConnect ADO. NET
[software reverse - basic knowledge] analysis method, assembly instruction architecture
[PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
Containerd series - detailed explanation of plugins
MindFusion. Virtual Keyboard for WPF
Clickpaas low code platform
[untitled]