Leetcode[232] Implement Queue using Stacks
29 Nov 2015###Task1 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. 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).
###Python
class Queue(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack1 = []
self.stack2 = []
def push(self, x):
"""
:type x: int
:rtype: nothing
"""
self.stack1.append(x)
def pop(self):
"""
:rtype: nothing
"""
if len(self.stack2):
return self.stack2.pop()
else:
while len(self.stack1):
self.stack2.append(self.stack1.pop())
return self.stack2.pop()
def peek(self):
"""
:rtype: int
"""
if len(self.stack2):
return self.stack2[-1]
else:
while len(self.stack1):
self.stack2.append(self.stack1.pop())
return self.stack2[-1]
def empty(self):
"""
:rtype: bool
"""
return len(self.stack1) == 0 and len(self.stack2) == 0
####Single stack
class Queue:
# initialize your data structure here.
def __init__(self):
self.stack = []
# @param x, an integer
# @return nothing
def push(self, x):
swap = []
while self.stack:
swap.append(self.stack.pop())
swap.append(x)
while swap:
self.stack.append(swap.pop())
# @return nothing
def pop(self):
self.stack.pop()
# @return an integer
def peek(self):
return self.stack[-1]
# @return an boolean
def empty(self):
return len(self.stack) == 0
###Java
class MyQueue {
private Stack<Integer> stack1 = new Stack<Integer>();
private Stack<Integer> stack2 = new Stack<Integer>();
public void push(int x) {
stack1.push(x);
}
public void pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
stack2.pop();
}
public int peek() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}
###Points
Double Stack
双栈法:
维护两个栈inStack与outStack,其中inStack接收push操作新增的元素,outStack为pop/peek操作提供服务
由于栈具有后进先出(Last In First Out)的性质,栈A中的元素依次弹出并压入空栈B之后,栈A中元素的顺序会被逆转
当执行pop或者peek操作时,如果outStack中元素为空,则将inStack中的所有元素弹出并压入outStack,然后对outStack执行相应操作
由于元素至多只会从inStack向outStack移动一次,因此peek/pop操作的平摊开销为O(1)
Single stack
单栈法:
在执行push操作时,使用辅助栈swap,将栈中元素顺序按照push顺序的逆序存储。
此时,push操作的时间复杂度为O(n),其余操作的时间复杂度为O(1)