代码拉取完成,页面将自动刷新
// Time: O(1), amortized
// Space: O(n)
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
A_.emplace(x);
}
// Removes the element from in front of queue.
void pop(void) {
peek();
B_.pop();
}
// Get the front element.
int peek(void) {
if (B_.empty()) {
// Transfers the elements in A_ to B_.
while (!A_.empty()) {
B_.emplace(A_.top());
A_.pop();
}
}
if (B_.empty()) { // B_ is still empty!
throw length_error("empty queue");
}
return B_.top();
}
// Return whether the queue is empty.
bool empty(void) {
return A_.empty() && B_.empty();
}
private:
stack<int> A_, B_;
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。