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

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

-- coding:utf-8 --

class Solution:
def init(self):
self.stackA = []
self.stackB = []

def push(self, node):
    # write code here
    self.stackA.append(node)
     
def pop(self):
    # return xx
    if self.stackB:
        return self.stackB.pop()
    elif not self.stackA:
        return None
    else:
        while self.stackA:
            self.stackB.append(self.stackA.pop())
        return self.stackB.pop()
发布了34 篇原创文章 · 获赞 4 · 访问量 1563

猜你喜欢

转载自blog.csdn.net/qq_36495569/article/details/103154242