题目描述
blablabla
样例
blablabla
算法1
C++ 代码
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
stack<int> stk, cache;
void push(int x) {
stk.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(!cache.empty()){
int x=cache.top();
cache.pop();
return x;
}else{
while(!stk.empty()){
int x=stk.top();
stk.pop();
cache.push(x);
}
int x=cache.top();
cache.pop();
return x;
}
}
/** Get the front element. */
int peek() {
if(!cache.empty()){
return cache.top();
}else{
while(!stk.empty()){
int x=stk.top();
stk.pop();
cache.push(x);
}
return cache.top();
}
}
/** Returns whether the queue is empty. */
bool empty() {
return cache.empty()&&stk.empty();
}
};