题解 | #火车进站#

火车进站

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

class IOS:
    def __init__(self):
        self.lst = []

    def main(self, lst01, lst02, lst03):
        """
        迭代求方案: 过程必须切片不能操作表
        :param lst01: 等待入栈
        :param lst02: 栈
        :param lst03: 出栈顺序记录
        :return: lst出栈顺序汇总
        """
        if len(lst01) == 0 and len(lst02) == 0:
            self.lst.append(lst03)  # 获取一种方案
        if len(lst01) > 0:  # 可以入栈
            self.main(lst01[1:], lst02 + [lst01[0]], lst03[:])
        if len(lst02) > 0:  # 可以出栈
            self.main(lst01[:], lst02[:-1], lst03 + [lst02[-1]])

    def result(self):
        self.lst.sort()
        for i in self.lst:
            for j in i:
                print(str(j), end=" ")
            print()


if __name__ == "__main__":
    n = int(input())
    l = list(map(int, input().split()))
    ob = IOS()
    ob.main(l, [], [])
    ob.result()

全部评论

相关推荐

迷茫的大四🐶:自信一点,我认为你可以拿到50k,低于50k完全配不上你的能力,兄弟,不要被他们骗了,你可以的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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