class MyQueue(object):
def __init__(self):
self.stackIn=[]
self.stackOut=[]
def push(self, x):
"""
:type x: int
:rtype: None
"""
self.stackIn.append(x)
def pop(self):
"""
:rtype: int
"""
while len(self.stackIn)>0:
self.stackOut.append(self.stackIn.pop())
num=self.stackOut.pop()
while len(self.stackOut)>0:
self.stackIn.append(self.stackOut.pop())
return num
def peek(self):
"""
:rtype: int
"""
while len(self.stackIn)>0:
self.stackOut.append(self.stackIn.pop())
num=self.stackOut.pop()
self.stackOut.append(num)
while len(self.stackOut)>0:
self.stackIn.append(self.stackOut.pop())
return num
def empty(self):
"""
:rtype: bool
"""
if len(self.stackIn)==0:
return True
return False
# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()
知识点
初始化列表:arr = []
在列表末尾添加元素 x:arr.append(x)
删除列表最后一个元素:arr.pop()