232. 用栈实现队列
232. 用栈实现队列
https://leetcode-cn.com/problems/implement-queue-using-stacks/
使用栈实现队列的下列操作:
push(x) -- 将一个元素放入队列的尾部。
pop() -- 从队列首部移除元素。
peek() -- 返回队列首部的元素。
empty() -- 返回队列是否为空。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
- 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
- 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
- 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
通过次数49,363 | 提交次数77,019
Second Try
2020-06-24
参考题解了解的快速插入,优化pop版本,结果时间排名也不见得有多快。
tricky部分有两个,一是pop的时候第二个栈保存倒序之后就维持原状了;二是peek方法中front元素的跟踪,不然为了实现这么个peek会很麻烦。
class Stack(object):
def __init__(self):
self.stack = []
def push(self, x):
self.stack.append(x)
def pop(self):
assert not self.empty(), "pop not allowed when empty"
return self.stack.pop()
def peek(self):
assert not self.empty(), "peek not allowed when empty"
return self.stack[-1]
def size(self):
return len(self.stack)
def empty(self):
return self.size() == 0
class MyQueue(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.sone = Stack()
self.stwo = Stack()
self.front = "" # 这个跟踪方法还是挺特别的
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: None
"""
if self.sone.empty():
self.front = x
self.sone.push(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
assert not self.empty(), "pop not allowed in empty queue"
if not self.stwo.empty():
return self.stwo.pop()
while not self.sone.empty():
self.stwo.push(self.sone.pop())
return self.stwo.pop()
def peek(self):
"""
Get the front element.
:rtype: int
"""
if not self.stwo.empty():
return self.stwo.peek()
return self.front # trick
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return self.sone.empty() and self.stwo.empty()
- 执行用时:20 ms, 在所有 Python 提交中击败了60.78%的用户
- 内存消耗:12.7 MB, 在所有 Python 提交中击败了100.00%的用户
First Try
2020-06-24
使用两个栈来实现队列,插入部分比较耗时,需要在两个栈之间倒腾两次,取出部分非常自然。
class Stack(object):
def __init__(self):
self.stack = []
def push(self, x):
self.stack.append(x)
def pop(self):
assert not self.empty(), "pop not allowed when empty"
return self.stack.pop()
def peek(self):
assert not self.empty(), "peek not allowed when empty"
return self.stack[-1]
def size(self):
return len(self.stack)
def empty(self):
return self.size() == 0
class MyQueue(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.sone = Stack()
self.stwo = Stack()
self.stacks = [self.sone, self.stwo]
self.idx = 0
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: None
需要倒腾两次,实在太浪费了
"""
while not self.stacks[self.idx].empty():
self.stacks[1 - self.idx].push(self.stacks[self.idx].pop())
self.stacks[self.idx].push(x)
while not self.stacks[1 - self.idx].empty():
self.stacks[self.idx].push(self.stacks[1 - self.idx].pop())
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
return self.stacks[self.idx].pop()
def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.stacks[self.idx].peek()
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return self.stacks[self.idx].empty()
# 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()
- 执行用时:16 ms, 在所有 Python 提交中击败了85.19%的用户
- 内存消耗:12.8 MB, 在所有 Python 提交中击败了100.00%的用户