LeetCode 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks.
- push(x) -- Push element x to the back of queue.
- pop() -- Removes the element from in front of queue.
- peek() -- Get the front element.
- empty() -- Return whether the queue is empty.
Notes:
- You must use only standard operations of a stack -- which means only
push,
to toppeek/pop from top,size,
andis emptyoperations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
//题目描写叙述:
//使用栈实现队列的下列操作:
//push(x) --将元素x加至队列尾部
//pop( ) --从队列头部移除元素
//peek( ) --获取队头元素
//empty( ) --返回队列是否为空
//注意:你仅仅能够使用栈的标准操作,这意味着仅仅有push to top(压栈), peek / pop from top(取栈顶 / 弹栈顶),
//以及empty(推断是否为空)是同意的。取决于你的语言,stack可能没有被内建支持。 你能够使用list(列表)或者deque(双端队列)来模拟。
//确保仅仅使用栈的标准操作就可以,你能够假设全部的操作都是有效的(比如,不会对一个空的队列运行pop或者peek操作) //解题方法:用两个栈就能够模拟一个队列,基本思路是两次后进先出 = 先进先出,
//元素入队列总是入in栈。元素出队列假设out栈不为空直接弹出out栈头元素。
//假设out栈为空就把in栈元素出栈全部压入out栈。再弹出out栈头。这样就模拟出了一个队列。 //核心就是保证每一个元素出栈时都经过了in,out两个栈,这样就实现了两次后进先出=先进先出。
class Queue {
public:
stack<int> in;
stack<int> out;
void move(){ //将in栈内的全部元素移动到out栈
while (!in.empty()){
int x = in.top();
in.pop();
out.push(x);
}
} // Push element x to the back of queue.
void push(int x) {
in.push(x);
} // Removes the element from in front of queue.
void pop(void) {
if (out.empty()){
move();
}
if (!out.empty()){
out.pop();
}
} // Get the front element.
int peek(void) {
if (out.empty()){
move();
}
if (!out.empty()){
return out.top();
}
} // Return whether the queue is empty.
bool empty(void) {
return in.empty() && out.empty();
}
};
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
LeetCode 232:Implement Queue using Stacks的更多相关文章
- 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 OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- LeetCode(232) Implement Queue using Stacks
题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- LeetCode算法题-Implement Queue Using Stacks(Java实现)
这是悦乐书的第195次更新,第201篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232).使用栈实现队列的以下操作. push(x) - 将元素x推 ...
- 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) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
随机推荐
- windows系统nexus3安装和配置
一.前言 为什么要在本地开发机器上安装nexus?首先声明公司内部是有自己的nexus仓库,但是对上传jar包做了限制,不能畅快的上传自己测试包依赖.于是就自己在本地搭建了一个nexus私服,即可以使 ...
- CODEVS 2455 繁忙的都市 SCOI2005(洛谷 P2330)
题目描述 Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两 ...
- Java笔记(十四) 并发基础知识
并发基础知识 一.线程的基本概念 线程表示一条单独的执行流,它有自己的程序计数器,有自己的栈. 1.创建线程 1)继承Thread Java中java.lang.Thread这个类表示线程,一个类可以 ...
- 潭州课堂25班:Ph201805201 django框架 第七课 常用 字段类型及参数,关系表的实现,表关系对象add,create的方法 (课堂笔记)
写个类,创建表格 执行命令生成文件 提交,在数据库中创建 进入数据库查看 对数据进行修改操作 只有用到 save() 才能触发 updaer_time 的更改, 下边 update 方法不会修改 up ...
- 一款好用的轮播插件swiper,适用于移动端和web
swiper的dom布局 <div id="commentsSwiper" class="swiper-container"> <div cl ...
- 解决 main(int argc, char** argv)这种情况下通过命令行传参,改为main函数里面给定参数。
本文是原创文章,未经允许,请勿转载. 原来程序中是通过运行exe,然后加上参数,然程序运行起来的.也就是main(int argc, char** argv)这里是通过argv参数是从命令行 传过来的 ...
- HTML5 学习02——新元素:canvas
HTML5 Canvas <canvas>标签:使用脚本 (通常是JavaScript)来绘制图形——默认情况下 <canvas> 元素没有边框和内容. 在画布上(Canvas ...
- pygame-KidsCanCode系列jumpy-part10-角色动画(上)
上一节学习如何利用spritesheet加载图片,但是player仍然是一张静态的图片,比较枯燥,我们要让它动起来! Player类,先把各种状态的图片加载起来: # 加载各种状态的图片序列 def ...
- Deepin 15.4 个性化设置
2017.10.03,开始使用 Deepin 15.4.1 桌面系统 Chrome 版本 60.0.3112.78(正式版本) (64 位) 1.开启 ls 别名: vim .bashrc 去掉以下代 ...
- ARCGIS做好的模型工具,已经设置为相对路径,拷贝后工具显示叉叉不可用的原因
.用户遇到此问题的场景: 用ModelBuilder构建了一系列的模型,其中有些模型很复杂,就把部分抽离出来,然后再整合到一起,我发现把整个模型工具箱拷贝到其他电脑上,这些复杂模型(里面除了系统自带的 ...