Leetcode 225,232 uses two queues to implement the stack, with two stacks to implement a queue
232 Implement Queue using Stacks My submissions Question
Total accepted:29497 Total submissions:86968 difficulty:easy
Implement the following operations of a queue using stacks.
**push (x) –push element x to the back of the queue.
Pop () –removes the element from in front of the 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 ope Rations is valid.
Depending on your language, stack may is not supported natively. You could simulate a stack by using a list or deque (double-ended queue), as long as if you have standard operations of a s Tack.
You may assume this all operations is valid (for example, no pop or peek operations would be called on an empty queue). * *
S1,S2 two stacks, if S2 is empty when out of queue, S1 elements all pop to S2 and then from S2 pop. otherwise directly s2 pops.
S1 push code directly into the queue
public class Queue {
stack<int> s1 = new stack<int> ();
stack<int> s2 = new stack<int> ();
Push element x to the back of the queue.
public void Push (int x) {
S1. Push (x);
}
Removes the element from front of the queue.
public void Pop () {
if (S2. count==0) {while
(S1. count>0) s2. Push (S1. Pop ());
}
S2. Pop ();
}
Get the front element.
public int Peek () {
if (S2. count==0) {while
(S1. count>0) s2. Push (S1. Pop ());
}
Return S2. Peek ();
}
Return whether the queue is empty.
public bool Empty () {
return S1. count==0 && S2. count==0;
}
}
**225 Implement Stack using Queues My submissions Question
Total accepted:27669 Total submissions:91210 difficulty:easy
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 the only standard operations of a queue–which means only push-to-back, peek/pop from front, size, and is empty Operations is valid.
Depending on your language, queue May is not supported natively. You could simulate a queue by using a list or deque (double-ended queue), as long as if you have standard operations of a Q Ueue.
You may assume this all operations is valid (for example, no pop or top operations would be called on an empty stack). * *
Feel this method is more stupid, do not know whether there is better. Always leave a queue empty. Queued for non-empty queues. Out of the queue, the non-empty queue is transferred all except the last one to the empty queue, and then out of the last non-empty queue. Code
public class Stack {queue q1=new queue (), q2=new queue ();
Push element x onto stack. public void Push (int x) {if (Q1. count!=0) Q1.
Enqueue (x); else if (Q2. count!=0) Q2.
Enqueue (x); else Q1.
Enqueue (x);
}//Removes the element on top of the stack. public void Pop () {Queue q = q1.
COUNT==0?Q2:Q1; Queue QQ = Q1.
COUNT==0?Q1:Q2; while (q.count>1) {qq.
Enqueue (Q.dequeue ());
} q.dequeue ();
}//Get the top element. public int Top () {Queue q = q1.
COUNT==0?Q2:Q1; Queue QQ = Q1.
COUNT==0?Q1:Q2; while (q.count>1) {qq.
Enqueue (Q.dequeue ());
} int ret = (int) q.peek (); Qq.
Enqueue (Q.dequeue ());
return ret;
}//Return whether the stack is empty. public bool Empty () {return Q1. Count==0 && Q2.
count==0; }
}