232. Implement Queue using Stacks
Link: https://leetcode.com/problems/implement-queue-using-stacks/
Description
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()
Returns true if the queue is empty, false otherwise.
Notes:
- You must use only standard operations of a stack, which means only
push to top
,peek/pop from top
,size
, andis empty
operations 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.
Approach
push(int x)
- Push the element to
stack1
.
pop()
- If stack2 is empty, move all the elements of
stack1
tostack2
. After that, the first element instack1
is the last emelentstack2
. Pop the last element. - If
stack2
is not empty, pop the last element.
Solution
class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty())
stack2.push(stack1.pop());
}
return 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();
}
}
/**
* 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
Link: https://leetcode.com/problems/implement-stack-using-queues/
Description
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()
Returns true if the stack is empty, false otherwise.
Notes:
- You must use only standard operations of a queue, which means that
onlypush to back
,peek/pop from front
,size
andis empty
operations
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.
Approach
Use 2 queues
push()
- Push the element in
stack1
.
pop()
- Move all the elments in
stack1
tostack2
, except the last element. - pop the last item, and let result equals to the last item.
- Let
stack1
equals tostack2
, and resetstack2
as an empty stack.
Use 1 queue
pop()
- compute the size of the
queue
. - traverse the
queue
, move the element on the top of thequeue
to the end of thequeue
except the last element of the originalqueue
. - Pop the last element.
Solution
//Use 1 queue
class MyStack {
Queue<Integer> queue1;
public MyStack() {
queue1 = new ArrayDeque<>();
}
public void push(int x) {
queue1.add(x);
}
public int pop() {
int size = queue1.size();
while (size > 1) {
int temp = queue1.remove();
queue1.add(temp);
size--;
}
return queue1.remove();
}
public int top() {
int size = queue1.size();
int result = -1;
while (size > 0) {
int temp = queue1.remove();
queue1.add(temp);
size--;
if (size == 0)
result = temp;
}
return result;
}
public boolean empty() {
return queue1.isEmpty();
}
}
/**
* 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();
*/
//Use 2 queues
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
queue1 = new ArrayDeque<>();
queue2 = new ArrayDeque<>();
}
public void push(int x) {
queue1.add(x);
}
public int pop() {
int temp = -1;
while(!queue1.isEmpty()) {
temp = queue1.remove();
if (!queue1.isEmpty())
queue2.add(temp);
}
Queue<Integer> tempQ = queue1;
queue1 = queue2;
queue2 = tempQ;
return temp;
}
public int top() {
int temp = -1;
while(!queue1.isEmpty()) {
temp = queue1.remove();
queue2.add(temp);
}
Queue<Integer> tempQ = queue1;
queue1 = queue2;
queue2 = tempQ;
return temp;
}
public boolean empty() {
return queue1.isEmpty();
}
}
/**
* 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();
*/