//在push的时候进行copy ,push,copy 优化读取操作copy次数
class MyQueue {
public:
/* Initialize your data structure here. /
stack[HTML_REMOVED] work, cache;
MyQueue() {
}
void copy(stack<int> &a, stack<int> &b) {
while (a.size()) {
b.push(a.top());
a.pop();
}
}
/** Push element x to the back of queue. */
void push(int x) {
copy(work,cache);
cache.push(x);
copy(cache,work);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(!work.empty()){
int value = work.top();
work.pop();
return value;
}
return -1;
}
/** Get the front element. */
int peek() {
if(!work.empty()){
int value = work.top();
return value;
}
return -1;
}
/** Returns whether the queue is empty. */
bool empty() {
return work.empty();
}
};
/*
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
/