美文网首页
用两个栈实现队列

用两个栈实现队列

作者: GoDeep | 来源:发表于2018-03-30 21:50 被阅读0次

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

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

    相关文章

      网友评论

          本文标题:用两个栈实现队列

          本文链接:https://www.haomeiwen.com/subject/qxrwcftx.html