【剑指Offer_4】用两个栈实现队列(Python)

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

# -*- coding:utf-8 -*-
class Solution:
    def __init__(self):
        self.s1 = []
        self.s2 = []
    def push(self, node):
        while self.s2:
            x = self.s2.pop()
            self.s1.append(x)
        self.s2.append(node)
        while self.s1:
            x = self.s1.pop()
            self.s2.append(x)
        
    def pop(self):
        xx = self.s2.pop()
        return xx

猜你喜欢

转载自blog.csdn.net/Vici__/article/details/104176436