LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stkPush.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int val;
if(stkPop.empty())
{
while(!stkPush.empty())
{
val=stkPush.top();
stkPush.pop();
stkPop.push(val);
}
}
val=stkPop.top();
stkPop.pop();
return val;
}
/** Get the front element. */
int peek() {
if(stkPop.empty())
{
while(!stkPush.empty())
{
int val=stkPush.top();
stkPush.pop();
stkPop.push(val);
}
}
return stkPop.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return stkPush.empty()&&stkPop.empty();
}
private:
stack<int> stkPush;
stack<int> stkPop;
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/
LeetCode 232 Implement Queue using Stacks 两个栈实现队列的更多相关文章
- 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 STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- 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 ...
- [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 ...
- 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 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 ...
随机推荐
- TFS自定义开发中的反射应用
最近CM(Configuration Management) 的同事在自定义开发TFS的过程中遇到一个问题. 领导要求快速开发一个工具, 可以自动连接TFS,然后自动Check out一些word文件 ...
- ElasticSearch logo 分布式搜索引擎 ElasticSearch
原文来自:http://www.oschina.net/p/elasticsearch Elastic Search 是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中 ...
- 6.JasperReports学习笔记6-jasperreports和ssh工程整合
转自:http://www.blogjava.net/vjame/archive/2013/10/12/404908.html 一.导入jasperreport相关jar包,这里采用当前比较稳定的5. ...
- shell入门-sed-1
sed这个工具比grep复杂一点,功能比grep复杂一点 功能也能指定匹配的行,不能颜色显示 sed 基础功能 [root@wangshaojun ~]# sed -n '10'p 1.txtuucp ...
- shell入门-sort排序
命令:sort 选项:-t:-kn 指定根据某段来排序 这里n代表数字,范围指定n,N.从n到N范围 -n 按数字顺序排列 -r 反序排列 -u 去重复排序 -un 数字顺序排列并去重复,系 ...
- CSS之边距合并
外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距.合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者. 情况1:当一个元素出现在另一个元素上面时,第一个元素的下外边距与第二个元 ...
- js中一些小知识点总结--持续更新
以下知识点来自于编写高质量代码-改善JavaScript程序的188个建议,只用于自我知识的补充. 一.NaN 1.NaN是一个特殊的数量值,不表示一个数字,尽管下面的代码仍然是返回类型为number ...
- jquery.html5uploader.js 上传控件
插件地址:http://blog.csdn.net/never_say_goodbye/article/details/8598521 先上个效果图: 相比来说,效果还是很不错的 使用MVC3做服务器 ...
- 【255】◀▶IEW-Unit20
Unit 20 Environment: Tourism I.定语从句及分词在雅思写作中的运用 定语从句: 1. 先行词 2. 关系词:关系代词.关系副词 3. 非限制性定语从句 4. 分词和定语从句 ...
- 26、HDF5 文件格式简介
转载:庐州月光 http://www.cnblogs.com/xudongliang/p/6907733.html 三代测序下机的原始数据不再是fastq格式了,而是换成了hdf5 格式,在做三代数据 ...