Implement Stack using Queues - LeetCode 225
2015-06-11 22:58
591 查看
题目描述:
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 push to back, peek/pop from front, size, and is empty operations 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.
Hide Tags Data Structure
分析:
由于栈是先进后出,队列是先进先出,两者出栈顺序是相反的,因此,因此两个队列来实现一个栈,一个作为中转队列,一个是目标队列,二者不断交换角色。过程描述如下:
在用队列实现栈的出栈操作时,需要删除队队尾的元素,那么得将目标队列的非队尾元素出队列存放到中转队列中,然后出队原队尾元素。此时目标队列和中转队列角色互换。
对于压栈操作,直接将元素放入目标队列(非空)队尾,若两个队列都为空,那么随便选择其中一个作为目标的队列即可。
对于判空,如过两个队列都为空,那么返回true,否则返回false。
对于top操作,直接返回非空队列的队尾元素即可。
思路还是挺简单的,以下是C++实现代码:
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 push to back, peek/pop from front, size, and is empty operations 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.
Hide Tags Data Structure
分析:
由于栈是先进后出,队列是先进先出,两者出栈顺序是相反的,因此,因此两个队列来实现一个栈,一个作为中转队列,一个是目标队列,二者不断交换角色。过程描述如下:
在用队列实现栈的出栈操作时,需要删除队队尾的元素,那么得将目标队列的非队尾元素出队列存放到中转队列中,然后出队原队尾元素。此时目标队列和中转队列角色互换。
对于压栈操作,直接将元素放入目标队列(非空)队尾,若两个队列都为空,那么随便选择其中一个作为目标的队列即可。
对于判空,如过两个队列都为空,那么返回true,否则返回false。
对于top操作,直接返回非空队列的队尾元素即可。
思路还是挺简单的,以下是C++实现代码:
/**///////////////////////////0ms*/ class Stack { public: queue<int> q1,q2; // Push element x onto stack. void push(int x) { if(q1.empty() && q2.empty() || !q1.empty()) q1.push(x); else q2.push(x); } // Removes the element on top of the stack. void pop() { int top = 0; if(!Stack::empty()){ if(!q1.empty()){ int n = q1.size()-1; while(n--) { q2.push(q1.front()); q1.pop(); } q1.pop(); } else{ int n = q2.size()-1; while(n--) { q2.push(q2.front()); q2.pop(); } q2.pop(); } } } // Get the top element. int top(){ if(!Stack::empty()){ if(!q1.empty()) return q1.back(); else return q2.back(); } } // Return whether the stack is empty. bool empty() { if(q1.empty() && q2.empty()) return true; return false; } };
相关文章推荐
- 在SQuirrel中使用Phoenix操作HBase——创建表和视图
- MYSQL优化浅谈,工具及优化点介绍,mysqldumpslow,pt-query-digest,explain等
- 黑马程序员_89_GUI图形化界面
- 【NGUI实例开发】实现弹窗面板
- UIPickerView - 城市选择
- UIDatePicker - 键盘处理-选择日期
- Java集合体系总结—Map、Set、List、Queue
- cocopods install或update 安装或更新第三方库时,报The ... target overrides the 'OTHER_LDFLAGS' build setting....
- Android UI常用实例 如何实现欢迎界面(Splash Screen)
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
- oc中UITableViewb的编辑插入模式
- NGUI研究院之为什么打开界面太慢(十三)
- uva 10099 The Tourist Guide(最“大”生成树)
- NGUI带位置图标的拖动例子
- VS2013编译GLUI
- UICamera的触摸事件
- 自定义控件UITypeEditor
- 安卓TV开发(三) 移动智能设备之实现主流TV电视盒子焦点可控UI
- 安卓TV开发(三) 移动智能设备之实现主流TV电视盒子焦点可控UI
- [LeetCode] Implement Stack using Queues