Journey of LeetCode|DAY 9
Preface
This is a new day to start my stack and queue journey.
Learn something new and keep reviewing what I learnt before.
1. Implement Queue using Stacks
LeetCode Link: 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() Returns true if the queue is empty, false otherwise.
Analysis and Solution
Double Stack
LeetCode C++ as followings Double Stack
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
// Import all stIn data only when stOut is empty
if (stOut.empty()) {
// Import all stIn data
while(!stIn.empty()) {//stIn is not empty
stOut.push(stIn.top());//Import all stIn data
stIn.pop();//
}
}
int result = stOut.top();//record value to result
stOut.pop();//pop
return result;
}
/** Get the front element. */
int peek() {
int res = this->pop(); // directly apply pop function
stOut.push(res); // add the value because pop function poped it
return res;//show the value
}
/** Returns whether the queue is empty. */
bool empty() {
return stIn.empty() && stOut.empty();//make a judgement
}
};
/**
* 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();
* bool param_4 = obj->empty();
*/
2. Implement Stack using Queues
LeetCode Link: 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() Returns true if the stack is empty, false otherwise.
Analysis and Solution
Double Queue
LeetCode C++ as followings Double Queue
class MyStack {
public:
queue<int> que1;
queue<int> que2; // assist queue; for backup
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que1.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que1.size();
size--;
while (size--) { // import que1 to que2;save the last element
que2.push(que1.front());//
que1.pop();
}
int result = que1.front(); // last element is the value that need to be return
que1.pop();
que1 = que2; // assign que2 to que1
while (!que2.empty()) { // empty que2
que2.pop();
}
return result;
}
/** Get the top element. */
int top() {
return que1.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return que1.empty();
}
};
/**
* 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();
* bool param_4 = obj->empty();
*/