题解 | #火车进站#

火车进站

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

def validateStackSequences(pushed, popped):
    tmp = []
    j = 0
    for i in range(len(pushed)):
        tmp.append(pushed[i])
        while tmp and tmp[-1]==popped[j]:
            tmp.pop()
            j += 1  
    return not tmp

def dfs(x):
    if x == len(nums)-1:
        
        res.append(list(nums))
        return 
    for i in range(x,len(nums)):
        nums[i],nums[x] = nums[x],nums[i]
        dfs(x+1)
        nums[i],nums[x] = nums[x],nums[i]

n = int(input())
nums = list(map(int,input().strip().split()))

res = []

dfs(0)

res = sorted(res)

for i in res:
    if validateStackSequences(nums, i):
        print(' '.join(list(map(str,i))))





全部评论

相关推荐

牛马人的牛马人生:一开始看成了网吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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