LeetCode OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式。
这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中。但是其他的实现也可以不是这样,可以是需要push的时候检查再,如果内容在stack2中,这时候将其倒回在进行push。这里采取第一种比较笨的方法,代码如下所示:
class Queue {
public:
// Push element x to the back of queue.
void push(int x) {
s1.push(x);
}
// Removes the element from in front of queue.
void pop(void) {
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
s2.pop();
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
}
// Get the front element.
int peek(void) {
while(!s1.empty()){
s2.push(s1.top());
s1.pop();
}
int ret = s2.top();
while(!s2.empty()){
s1.push(s2.top());
s2.pop();
}
return ret;
}
// Return whether the queue is empty.
bool empty(void) {
return s1.empty();
}
private:
stack<int> s1;
stack<int> s2;
};
LeetCode OJ: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 用栈来实现队列
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 ...
- leetCode(37):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(23)-Implement Queue using Stacks
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
随机推荐
- java红黑树
从这里学了一些知识点https://blog.csdn.net/sun_tttt/article/details/65445754,感谢作者
- $思维导图——numpy基本知识
- 为Android添加开机启动脚本
转:https://blog.csdn.net/u014316462/article/details/76438611 本文介绍了一种在Android 4.2.2源码中添加.修改文件或者代码,来达到使 ...
- MVC的局部视图传参的小技巧--见人才网头部导航
当我们设计一个局部视图时,当出现有类似导航的功能(如:选择左边的某个按钮跳到某个页,且顶部导航也作相印改变),如果我们选择把导航作为局部视图来处理,调用就可以做如下处理: @Html.RenderAc ...
- SqlMapConfig.xml配置
总结自:https://blog.csdn.net/d582693456/article/details/79886780 SqlMapConfig.xml是mybatis的核心配置 properti ...
- [转载]Javassist 使用指南(一)
======================= 本文转载自简书,感谢原作者!. 原链接如下:https://www.jianshu.com/p/43424242846b =============== ...
- 爬虫之JSON案例
糗事百科实例: 爬取糗事百科段子,假设页面的URL是 http://www.qiushibaike.com/8hr/page/1 要求: 使用requests获取页面信息,用XPath / re 做数 ...
- Apache Kafka之设计
转自: http://blog.csdn.net/kevin_hx001/article/details/9413565 http://kafka.apache.org/design.h ...
- erwin逆向工程,logical模型列名修改为中文
逆向工程,应该选择physical template,这样拷贝到logical/physical 模型中,才可以将logical模型的列名修改为中文.
- 【咖啡の设备】Wacaco的minipresso便携咖啡机(咖啡粉版,胶囊版)
之前看到houjy527发帖说入手了一台minipresso胶囊版,想起来这货是个好玩具,出差旅行必备佳品(可惜我从没出过差) minipresso设计很赞,简直不能更方便了~ 下面是houjy527 ...