代码拉取完成,页面将自动刷新
// Time: push: O(n), pop: O(1), top: O(1)
// Space: O(n)
class Stack {
public:
queue<int> q_;
// Push element x onto stack.
void push(int x) { // O(n)
q_.emplace(x);
for (int i = 0; i < q_.size() - 1; ++i) {
q_.emplace(q_.front());
q_.pop();
}
}
// Remove the element on top of the stack.
void pop() { // O(1)
q_.pop();
}
// Get the top element.
int top() { // O(1)
return q_.front();
}
// Return whether the stack is empty.
bool empty() { // O(1)
return q_.empty();
}
};
// Time: push: O(1), pop: O(n), top: O(1)
// Space: O(n)
class Stack2 {
public:
queue<int> q_;
int top_;
// Push element x onto stack.
void push(int x) { // O(1)
q_.emplace(x);
top_ = x;
}
// Remove the element on top of the stack.
void pop() { // O(n)
for (int i = 0; i < q_.size() - 1; ++i) {
top_ = q_.front();
q_.emplace(top_);
q_.pop();
}
q_.pop();
}
// Get the top element.
int top() { // O(1)
return top_;
}
// Return whether the stack is empty.
bool empty() { // O(1)
return q_.empty();
}
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。