As the title described, you should only use two stacks to implement a queue's actions.
The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.
Both pop and top methods should return the value of first element.
Example
push(1)
pop() // return 1
push(2)
push(3)
top() // return 2
pop() // return 2
解题思路:
push就是放入第一个stack, pop的话要先看第二个stack空不空,如果空的话就把第一个stack里的放入第二个stack,然后pop。top就是第二个stack的top.
class MyQueue {
public:
stack<int> stack1;
stack<int> stack2;
MyQueue() {
// do intialization if necessary
}
void push(int element) {
// write your code here
stack1.push(element);
}
int pop() {
// write your code here
if (stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
}
int popNum = stack2.top();
stack2.pop();
return popNum;
}
int top() {
// write your code here
if(stack2.empty()) {
while (!stack1.empty()) {
stack2.push(stack1.top());
stack1.pop();
}
}
return stack2.top();
}
};