题目链接
https://leetcode.cn/problems/implement-queue-using-stacks/
代码
class MyQueue(object):
def __init__(self):
self.stack_in = []
self.stack_out = []
def push(self, x):
self.stack_in.append(x)
def pop(self):
if self.empty():
return None
if self.stack_out:
return self.stack_out.pop()
else:
for i in range(len(self.stack_in)):
self.stack_out.append(self.stack_in.pop())
return self.stack_out.pop()
def peek(self):
num = self.pop()
self.stack_out.append(num)
return num
def empty(self):
return not (self.stack_in or self.stack_out)