232. Implement Queue using Stacks
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).
Implement the MyQueue class:
void push(int x)Pushes element x to the back of the queue.int pop()Removes the element from the front of the queue and returns it.int peek()Returns the element at the front of the queue.boolean empty()Returnstrueif the queue is empty,falseotherwise.
Notes:
- You must use only standard operations of a stack, which means only
push to top,peek/pop from top,size, andis emptyoperations are valid. - Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.
Example 1:
Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]
Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false
Constraints:
1 <= x <= 9- At most
100calls will be made topush,pop,peek, andempty. - All the calls to
popandpeekare valid.
用栈实现队列需要两个栈,一个进一个出。
代码:
class MyQueue {
public MyQueue() {
istack = new Stack<Integer>();
ostack = new Stack<Integer>();
}
public void push(int x) {
istack.push(x);
}
public int pop() {
if(ostack.empty()) {
while(!istack.empty()) {
Integer top = istack.pop();
ostack.push(top);
}
return ostack.pop();
}
else {
return ostack.pop();
}
}
public int peek() {
if(ostack.empty()) {
while(!istack.empty()) {
Integer top = istack.pop();
ostack.push(top);
}
return ostack.peek();
}
else {
return ostack.peek();
}
}
public boolean empty() {
return (istack.empty() && ostack.empty());
}
Stack<Integer> istack;
Stack<Integer> ostack;
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
225. Implement Stack using Queues
Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).
Implement the MyStack class:
void push(int x)Pushes element x to the top of the stack.int pop()Removes the element on the top of the stack and returns it.int top()Returns the element on the top of the stack.boolean empty()Returnstrueif the stack is empty,falseotherwise.
Notes:
- You must use only standard operations of a queue, which means that only
push to back,peek/pop from front,sizeandis emptyoperations are valid. - Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.
Example 1:
Input
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 2, 2, false]
Explanation
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // return 2
myStack.pop(); // return 2
myStack.empty(); // return False
Constraints:
1 <= x <= 9- At most
100calls will be made topush,pop,top, andempty. - All the calls to
popandtopare valid.
Follow-up: Can you implement the stack using only one queue?
这题使用两个queue比起使用一个queue并没有增加效率,TC,SC全都一样。用一个队列的话,在pop的时候要把队列的前n-1个元素依次pop并添加到队尾。然后pop
Deque的系统API很不熟,要多练。
代码:
class MyStack {
public MyStack() {
mq = new ArrayDeque<Integer>();
}
public void push(int x) {
mq.addLast(x);
}
public int pop() {
int size = mq.size();
while(size-- > 1) {
Integer cur = mq.peekFirst();
mq.addLast(cur);
mq.pollFirst();
}
return mq.pollFirst();
}
public int top() {
return mq.peekLast();
}
public boolean empty() {
return mq.isEmpty();
}
Deque<Integer> mq;
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/