Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty()
sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中
push与sa有关,而pop(),peek()与sb有关,即将sa输入元素出栈放到sb中(函数move).
为此,只有两个栈为空才能让队列为空
class Queue {
public:
// Push element x to the back of queue.
stack<int> sa;
stack<int> sb;
void move(){
while(!sa.empty()){
sb.push(sa.top());
sa.pop();
}
}
void push(int x) {
sa.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
if(!sb.empty()) sb.pop();
else{
move();
sb.pop();
}
}
// Get the front element.
int peek(void) {
if(!sb.empty()) return sb.top();
else{
move();
return sb.top();
}
}
// Return whether the queue is empty.
bool empty(void) {
return sa.empty() && sb.empty();
}
};
Leetcode 232 Implement Queue using Stacks STL的更多相关文章
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- android笔记:Notification通知的使用
通知(Notification),当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现. 发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后 ...
- Elasticsearch refresh vs. flush【转载】
源地址: http://www.jianshu.com/p/0e9f6346f1fe 问: 若一个新的文档索引进ES索引,则它在索引操作执行后约1s可以搜索到.然而我们可以直接调用_flush或 ...
- 完全背包问题 POJ1384
完全背包即物品的数量不收限制, 根据01背包的思想,因为每件物品只能选1个,则要求我们不能依赖已选择物品i的选项的时候,所以需要逆序遍历 则在完全背包问题中,我们需要正序遍历. 此题时要求求出最小价值 ...
- 实现的一个ajax请求组件 有加载效果
var zhanglei_Ajax = function(url,data,fn){ var str = '<div class="mask" style="pos ...
- NSDate获取当前时间,并且转化为需要的格式
NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter ...
- Verilog之串口(UART)通信
0:起始位,低电平:1~8:数据位:9:校验位,高电平:10:停止位,高电平. 波特率 “9600bps”表示每秒可以传输9600位. 波特率定时计数器由时钟频率除以波特率. 采集1~8位,忽略0.9 ...
- ueditor .net版本上传不了图片问题
百度的Ueditor适合中国人,所以,选择了它,可是恶梦才刚刚开始,最头痛的就是图片死活就是上传不成功,悲剧中.. 现在来分析下为什么报错:什么也不说了,上图
- 【洛谷P3398】仓鼠找sugar
画个图就能多少看出些规律 证明借鉴一下大牛的题解: 设从A到B,经过的深度最小的点为X 同理,C,D的为Y 题目是一个点从A出发到B 一个从C出发到D 那么从A到B可以分解成 先从A到X 再从X到B. ...
- Windows中多个python版本共存的问题
原创文章,未经本人允许进制转载. 在我的Windows中,先安装了python3.4,后来因为需要又安装了python2.7,结果发现: 直接双击1.py和命令行python 1.py使用的pytho ...
- php安装gearman扩展实现异步分步式任务
参考: 1.小喵爱你的博客 2.PHP Manual 依赖 1.gcc44 2.boost >=1.39 3.libevent 4.php5.3+ 5.update ld.so.conf 安装依 ...