注意:
你只能使用栈的标准操作:push to top,peek/pop from top, size 和 is empty;
如果你选择的编程语言没有栈的标准库,你可以使用list或者deque等模拟栈的操作;
输入数据保证合法,例如,在队列为空时,不会进行pop或者peek等操作;
数据范围
每组数据操作命令数量 [0,100]。
样例
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // returns 1
queue.pop(); // returns 1
queue.empty(); // returns false
class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> a, b;
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
a.push(x);
}
void copy(stack<int> &x, stack<int> &y){
while(x.size()){
y.push(x.top());
x.pop();
}
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
copy(a,b);
int res = b.top();
b.pop();
copy(b,a);
return res;
}
/** Get the front element. */
int peek() {
copy(a,b);
int res = b.top();
copy(b,a);
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return a.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();
*/
非常开朗的网友哈哈哈哈
鹅鹅鹅
哈哈哈哈
一打开acwing就99+
你们……
支持!
支持!
这个题不用class也能解决
我没用
应该不行
用这道题的源代码都可以运行成功
但提交以后就错误
必须要用
栈和队列是一个数据结构吗
是的
这个我有点学不懂 呜呜
旺柴
、????
认真一点,你可以看y总的视频讲解
看什么看啊
去洛谷做题
那里是很受欢迎的一个平台
做《》
你好兄弟,咱们拼团算法提高课吧
《栈和队列是一个数据结构吗?是的》
pinle
这个y总说过@Wang JY
tuthyt。
luogu
你们……
我们这下了中雨……