难度: Easy
原题连接
内容描述
Implement the following operations of a queue using stacks.
push(x) -- Push element x to the back of queue.
pop() -- Removes the element from in front of queue.
peek() -- Get the front element.
empty() -- Return whether the queue is empty.
Example:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
Notes:
You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
思路 1 - 时间复杂度: O(1) + O(N) + O(1) + O(1)- 空间复杂度: O(N)******
class Queue(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.lst = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.lst.append(x)
def pop(self):
"""
:rtype: nothing
"""
del self.lst[0]
def peek(self):
"""
:rtype: int
"""
return self.lst[0]
def empty(self):
"""
:rtype: bool
"""
return not self.lst
思路 2 - 时间复杂度: O(N) + O(1) + O(1) + O(1)- 空间复杂度: O(N)******
用两个stack来模拟, beats 100%
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.s1 = []
self.s2 = []
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
while self.s1:
self.s2.append(self.s1.pop())
self.s1.append(x)
while self.s2:
self.s1.append(self.s2.pop())
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
return self.s1.pop()
def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.s1[-1]
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return not self.s1
思路 3 - 时间复杂度: O(1) + O(1) + O(1) + O(1)- 空间复杂度: O(N)******
用两个stack来模拟, 换一种方式,beats 100%
时间复杂度参考Solutions
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.s1 = []
self.s2 = []
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.s1.append(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
self.peek()
return self.s2.pop()
def peek(self):
"""
Get the front element.
:rtype: int
"""
if not self.s2:
while self.s1:
self.s2.append(self.s1.pop())
return self.s2[-1]
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return not self.s1 and not self.s2