用两个栈来实现一个队列,完成队列的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==[]:
while self.StackA:
self.StackB.append(self.StackA.pop())
return self.StackB.pop()
return self.StackB.pop()
思路:
- 栈A用来作入队列
- 栈B用来出队列,当栈B为空时,栈A全部出栈到栈B,栈B再出栈(即出队列)
2071

被折叠的 条评论
为什么被折叠?



