代码:
class Solution:
from collections import deque
def __init__(self):
self.queue1, self.queue2 = deque(), deque()
# 用两个deque实现push操作
def push(self, x):
self.queue2.append(x)
while self.queue1:
self.queue2.append(self.queue1.popleft())
self.queue1, self.queue2 = self.queue2, self.queue1
# 用一个deque实现push操作
def push(self, x):
self.queue1.append(x)
for i in range(len(self.queue1)-1):
self.queue1.append(self.queue1.popleft())
def pop(self):
return self.queue1.popleft()
def top(self): # 取队首元素
return self.queue1[0]
def empty(self):
return not self.queue1
网友评论