题解 | #火车进站#

火车进站

https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109

n = int(input())
L = list(map(int, input().split()))
ans = []


def func(wait, stack, out):
    if len(out) == n:
        ans.append(" ".join(map(str, out)))
    if len(wait) != 0:
        func(wait[1:], stack + [wait[0]], out)
    if len(stack) != 0:
        func(wait, stack[:-1], out + [stack[-1]])


func(L, [], [])
for item in sorted(ans):
    print(item)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务