Offer- wins the two stacks to implement a queue - a queue and stack -python

Two stacks to achieve a queue, the completion queue Push and Pop operations. Queue elements int.

 

Ideas: the use of two stacks, stackA node for receiving

stackB for receiving stackA of the stack
# -*- 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()

 

Guess you like

Origin www.cnblogs.com/ansang/p/11884737.html