您的位置:首页 > 产品设计 > UI/UE

LeetCode "Implement Stack using Queues"

2015-06-16 04:35 561 查看
Two-queue solution

class Stack {
queue<int> q;
queue<int> q0;
int _top;
public:
// Push element x onto stack.
void push(int x) {
q.push(x);
_top = x;
}

// Removes the element on top of the stack.
void pop() {
auto len0 = q.size();
while(--len0)
{
q0.push(q.front());
q.pop();
}
q.pop();
while(!q0.empty())
{
q.push(_top = q0.front());
q0.pop();
}
}

// Get the top element.
int top() {
return _top;
}

// Return whether the stack is empty.
bool empty() {
return q.empty();
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: