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); } // ...
随机推荐
- Spring MVC 复习笔记03
1. @RequestMapping 1). url映射 定义controller方法对应的url,进行处理器映射使用. 2). 窄化请求映射 3). 限制http请求方法 出于安全性考虑,对htt ...
- mac/linux查询网络端口占用
参考:http://www.cnblogs.com/kaiye/archive/2013/05/25/3099393.html netstat命令 netstat -an|grep 8080 lsof ...
- 用74HC165读8个按键状态
源:用74HC165读8个按键状态 源:74LV165与74HC595 使用 74LV165说明: 74LV165是8位并行负载或串行输入移位寄存器,末级提供互补串行输出(Q7和Q7).并行负载(PL ...
- cl.exe 命令行编译sqlite3 sqlite3.dll及sqlite3.exe
有点被宇宙最强的ide惯坏了,封装的太好,不能像gcc那样一步步了解其原理,其实强大的vs背后也有类似gcc的cl.exe 看到How To Compile SQLite http://sqlite. ...
- Exynos4412 IIC总线驱动开发(一)—— IIC 基础概念及驱动架构分析
关于Exynos4412 IIC 裸机开发请看 :Exynos4412 裸机开发 —— IIC总线 ,下面回顾下 IIC 基础概念 一.IIC 基础概念 IIC(Inter-Integrated Ci ...
- kali rolling 配置网络
一. 在文件系统里找到/etc/network下的interfaces文件,打开后可以看到eth0为dhcp,将其修改为如下形式: # This file describes the network ...
- nfs报错 - No route to host
nfs报错 - No route to host ______________________________ 因为防火墙阻止的原因. 解决方法:服务器rhel7系统下,打开firewall-conf ...
- Elasticsearch Server,2nd Edition pdf 翻译 中文
很偶然的机会,就需要接触到搜索,入门就是google trend已然超过solr的ES.在入门的时候找书的时候发现没有中文版的.于是自己开始翻译Elasticsearch Server,2nd Edi ...
- vue集成ueditor
相关代码见github 1.引入ueditor相关的文件,具体目录见下图如下 我将下载的文件放在static下面,这里专门用来放置相关的静态文件 (在ueditor.config.js需要配置一下路径 ...
- AngularJS Injector和Service的工作机制
要了解angularJS里的injector和Service是如何工作的,需要阅读/src/auto/injector.js.另外要结合/src/loader.js才能明白它的应用场景. auto/i ...