AcWing 20. 用两个栈实现队列 - Python3
原题链接
简单
作者:
KYCygni
,
2021-04-04 04:31:26
,
所有人可见
,
阅读 346
Python3 代码
from collections import deque
class MyQueue(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = deque()
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.stack.append(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if self.stack:
return self.stack.popleft()
else:
return None
def peek(self):
"""
Get the front element.
:rtype: int
"""
if self.stack:
return self.stack[0]
else:
return None
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
if self.stack:
return len(self.stack) == 0
else:
return True