225.Implement Stack using Queues
2015-06-27 10:47
302 查看
Implement the following operations of a stack using queues.
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Notes:
You must use only standard operations of a queue -- which means only
and
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
class Stack {
private:
queue<int> myQueue1;
queue<int> myQueue2;
public:
// Push element x onto stack.
void push(int x) {
myQueue1.push(x);
}
// Removes the element on top of the stack.
void pop() {
int eleNum = myQueue1.size();
while(eleNum > 1)
{
myQueue2.push(myQueue1.front());
myQueue1.pop();
eleNum--;
}
myQueue1.pop();
eleNum = myQueue2.size();
while(eleNum > 0)
{
myQueue1.push(myQueue2.front());
myQueue2.pop();
eleNum--;
}
}
// Get the top element.
int top() {
int eleNum = myQueue1.size();
while(eleNum > 1)
{
myQueue2.push(myQueue1.front());
myQueue1.pop();
eleNum--;
}
int ret = myQueue1.front();
myQueue2.push(myQueue1.front());
myQueue1.pop();
eleNum = myQueue2.size();
while(eleNum > 0)
{
myQueue1.push(myQueue2.front());
myQueue2.pop();
eleNum--;
}
return ret;
}
// Return whether the stack is empty.
bool empty() {
return myQueue1.empty();
}
};
push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.
Notes:
You must use only standard operations of a queue -- which means only
push to back,
peek/pop from front,
size,
and
is emptyoperations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
class Stack {
private:
queue<int> myQueue1;
queue<int> myQueue2;
public:
// Push element x onto stack.
void push(int x) {
myQueue1.push(x);
}
// Removes the element on top of the stack.
void pop() {
int eleNum = myQueue1.size();
while(eleNum > 1)
{
myQueue2.push(myQueue1.front());
myQueue1.pop();
eleNum--;
}
myQueue1.pop();
eleNum = myQueue2.size();
while(eleNum > 0)
{
myQueue1.push(myQueue2.front());
myQueue2.pop();
eleNum--;
}
}
// Get the top element.
int top() {
int eleNum = myQueue1.size();
while(eleNum > 1)
{
myQueue2.push(myQueue1.front());
myQueue1.pop();
eleNum--;
}
int ret = myQueue1.front();
myQueue2.push(myQueue1.front());
myQueue1.pop();
eleNum = myQueue2.size();
while(eleNum > 0)
{
myQueue1.push(myQueue2.front());
myQueue2.pop();
eleNum--;
}
return ret;
}
// Return whether the stack is empty.
bool empty() {
return myQueue1.empty();
}
};
相关文章推荐
- UITextView自适应内容的高度且实现光标总是距离键盘一段距离而滑动条却可以延伸到光盘
- require.js的用法
- Android UiAutomator 自动化环境搭建
- iOS 多线程开发之OperationQueue(二)NSOperation VS GCD
- easy=ui 树形结构案例
- 关于UIImageView不显示图片的问题小记
- UIButton 设置阴影效果
- iOS UIView非常用方法及属性详解
- ueditor 长文章分页
- Win10 Build 10154全新壁纸:海滩上奔跑的美女
- Windows 10 Build 10149手机预览版中文评测视频
- ios上 更改 状态栏(UIStatusBar)的颜色
- Windows 10 build 10154更新日志 神秘刮刮乐现身
- Windows 10 Build 10154最新版系统截图首曝光
- 深入浅出Cocoa多线程编程之 block 与 dispatch quene
- 深入浅出Cocoa多线程编程之 block 与 dispatch quene
- 深入浅出Cocoa多线程编程之 block 与 dispatch quene
- GPUImage在摄像头拍照中多滤镜的使用
- Google interview question: 2-D range sum query(2-D segment tree)
- #leetcode#Distinct Subsequences