题目描述:用两个栈来实现一个队列,完成队列的push和pop操作,队列中的元素为int类型
解题思路:栈是先进后出,队列是先进先出,栈A用来作入队列,栈B用来出队列,当栈B为空时,栈A全部出栈到栈B,栈B再出栈(即出队列)
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()