225. Implement Stack using Queues
2016-07-08 21:18
447 查看
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.
用一个队列实现栈。
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.
用一个队列实现栈。
class Stack { queue<int> Q; public: // Push element x onto stack. void push(int x) { Q.push(x); } // Removes the element on top of the stack. void pop() { for (int i=0; i < Q.size()-1; i++) { Q.push(Q.front()); Q.pop(); } Q.pop(); } // Get the top element. int top() { return Q.back(); } // Return whether the stack is empty. bool empty() { return Q.empty(); } };
相关文章推荐
- ntp的错误 no server suitable for synchronization found
- CodeForces 625A E - Guest From the Past
- CodeForces 626A B - Robot Sequence
- iOS UITabBar属性详解 UITabBarController 底部 UITabBarItem 间距调整
- hdu5286 wyh2000 and sequence
- SOAPUI使用教程-REST请求工作
- android studio关于build/outputs/apk/中apk或arr或jar无法删除的解决方法
- hdu 5063 Operation the Sequence(思路)
- UIweib的简单实用
- 将UIview描画成虚线等.
- 去掉UITableView多余的空白行分割线
- js 用defaultValue 模拟重置键 reset
- QOS-4 Queue队列机制-2 CBWFQ and LLQ
- QOS-3 Queue 队列机制
- UVA - 424 Integer Inquiry
- JUC源码分析19-队列-PriorityBlockingQueue
- Android UI性能问题探讨
- iOS UITextView 纯英文排版换行出错的解决方案
- dump View Hierarchy for UI automator
- UITableView的内容视图在有无导航栏下的区别