其他分享
首页 > 其他分享> > 剑指offer-用两个栈实现队列05

剑指offer-用两个栈实现队列05

作者:互联网

class Solution:
    def __init__(self):
        self.stackpush=[]
        self.stackpop=[]
    def push(self, node):
        # write code here
        self.stackpush.append(node)
    def pop(self):
        # return xx
        if len(self.stackpop)==0:
            while len(self.stackpush) is not 0:
                self.stackpop.append(self.stackpush.pop(-1))
        else:
            return self.stackpop.pop(-1)
        return self.stackpop.pop(-1)

思路:设置两个栈。stackpush用于入队列,stackpop用于出队列,当栈stackpop不为空时,直接出队列。当stackpop为空时,把stackpush出栈,stackpop入栈。直到stackpush为空,之后再出队列栈,完成出队列

标签:return,offer,队列,self,05,stackpop,pop,stackpush
来源: https://www.cnblogs.com/zhaiyansheng/p/10411349.html