题目链接
https://leetcode.com/problems/implement-stack-using-queues/
题目原文
Implement the following operations of a stack using queues.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- empty() – Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
- Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
题目翻译
太长不翻了。大概就是用队列的push to back, peek/pop from front, size, is empty方法,实现栈的push, pop, top, empty方法。
思路方法
镜像问题:232. Implement Queue using Stacks
思路一
队列是先进先出,每次出只能出队列的头部,而栈是后进先出,所以可以想办法每次把入队的元素弄到队列头部。于是可以考虑在每次push到队列的时候对其他元素做个重新pop和push将当前元素转移到队头。
该方法需要一个队列,push的复杂度O(n),pop的复杂度O(1)。
代码
class Stack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.stack.insert(0, x)
for i in range(len(self.stack)-1):
self.stack.insert(0, self.stack[-1])
self.stack.pop()
def pop(self):
"""
:rtype: nothing
"""
self.stack.pop()
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def empty(self):
"""
:rtype: bool
"""
return not self.stack
思路二
也可以用两个队列实现,将思路一的过程分开。但这时要引入一个变量来记录栈顶元素。
该方法需要两个队列,push的复杂度O(1),pop的复杂度O(n)。
代码
class Stack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.queue1 = []
self.queue2 = []
self.topx = None
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.queue1.insert(0, x)
self.topx = x
def pop(self):
"""
:rtype: nothing
"""
while len(self.queue1) > 1:
self.topx = self.queue1.pop()
self.queue2.insert(0, self.topx)
self.queue1.pop()
self.queue1, self.queue2 = self.queue2, self.queue1
def top(self):
"""
:rtype: int
"""
return self.topx
def empty(self):
"""
:rtype: bool
"""
return not self.queue1
思路三
用两个队列,push的复杂度O(n),pop的复杂度O(1)。
代码
class Stack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.queue1 = []
self.queue2 = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.queue2.insert(0, x)
while self.queue1:
self.queue2.insert(0, self.queue1.pop())
self.queue1, self.queue2 = self.queue2, self.queue1
def pop(self):
"""
:rtype: nothing
"""
self.queue1.pop()
def top(self):
"""
:rtype: int
"""
return self.queue1[-1]
def empty(self):
"""
:rtype: bool
"""
return not self.queue1
PS: 新手刷LeetCode,新手写博客,写错了或者写的不清楚还请帮忙指出,谢谢!
转载请注明:http://blog.csdn.net/coder_orz/article/details/51605052