[leetcode] Implement Stack using Queues
2015-07-31 16:12
549 查看
题目链接在此。
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).
用2个队列:所有数据放在第一个队列,pop和top操作时要用到第二个队列进行暂时存储,最后第二个队列中的数据还是要按顺序放回第一个队列。
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 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).
用2个队列:所有数据放在第一个队列,pop和top操作时要用到第二个队列进行暂时存储,最后第二个队列中的数据还是要按顺序放回第一个队列。
class Stack { public: // Push element x onto stack. void push(int x) { a.push(x); } // Removes the element on top of the stack. void pop() { if (empty()) { cout << "The stack is empty!\n"; return ; } while (a.size() != 1) { int tmp = a.front(); a.pop(); b.push(tmp); } a.pop(); while (!b.empty()) { int tmp = b.front(); b.pop(); a.push(tmp); } } // Get the top element. int top() { if (empty()) { cout << "The stack is empty!\n"; return -1; } int head; while (!a.empty()) { head = a.front(); a.pop(); b.push(head); } while (!b.empty()) { int tmp = b.front(); b.pop(); a.push(tmp); } return head; } // Return whether the stack is empty. bool empty() { return a.empty() && b.empty(); } private: queue<int> a; queue<int> b; };
相关文章推荐
- UIView常用的一些方法小记之setNeedsDisplay和setNeedsLayout
- break和continue
- UI控件---CollectionView
- MenuInflater用法
- 设置UITableviewCell的separatorStyle左边顶格
- easyui combogrid 的查询
- 响应式web设计之CSS3 Media Queries
- Target runtime com.genuitec.runtime.generic.jee50 is not defined
- ue怎样取消保存生成的.bak文件
- HDOJ 题目4339 Query(线段树,单点更新)
- ubuntu-64位 error while loading shared libraries: libuuid.so.1: wrong ELF class: ELFCLASS64
- iOS开发 扩大UIButton的响应区域
- measure_circuit_width_lines_gauss.hdev检测pcb线宽 相关例程学习
- requireJs详解(二)
- POJ 3080 Blue Jeans(字符串处理)
- sqlserver 生成UUID随机码
- select的option属性value以及text值的获取操作
- 基于ueditor开发的仿QQ空间照片上传功能
- [leetcode] Implement Queue using Stacks
- Optional int parameter 'page' is present but cannot be translated into a null value due to being dec