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 top
    peek/pop from topsize,
    and is empty operations 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的更多相关文章

  1. LeetCode 232: Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  2. LeetCode OJ:Implement Queue using Stacks(栈实现队列)

    比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...

  3. 【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( ...

  4. 【LeetCode OJ 232】Implement Queue using Stacks

    题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...

  5. LeetCode(232) Implement Queue using Stacks

    题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...

  6. LeetCode算法题-Implement Queue Using Stacks(Java实现)

    这是悦乐书的第195次更新,第201篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232).使用栈实现队列的以下操作. push(x) - 将元素x推 ...

  7. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  8. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  9. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

随机推荐

  1. Linux ubantu中安装虚拟/使用环境virtualenv以及python flask框架

    今天学习了python flask框架的安装过程以及使用案例,感觉网上讲的东西都没有从我们这种初学者的角度去考虑(哈哈),最后还是奉上心得: 1.安装virtualenv $ sudo apt-get ...

  2. Intellij IDEA更改项目优先编译顺序

    今天启动公司项目突然发现了一个问题,类里面明明有这个方法,但是无论怎样都不编译.(由于公司项目是二次开发,代码都是第三方写的,代码请勿仔细看,怕伤了你的眼睛.) 如图,找不到setJGMC方法. 但是 ...

  3. android 职业 转行

    不知道多少人和我一样.学安卓安卓工作.成了一件很烦躁的事情.甚至迷茫.    起初学安卓,是因为安卓流行,所以有兴趣,想要学.那个时候想做一个应用,想对安卓手机有个了解,比如获取手机短信.没有太在意工 ...

  4. bzoj3033: 太鼓达人 欧拉路径

    题目链接 bzoj3033: 太鼓达人 题解 对于k-1位点,k位二进制位边,将点的转移连起来 每个点的入度和出度相等并且全部是偶点 只需要在这个图中找字典序最小的欧拉回路 可以贪心地找字典序较小的边 ...

  5. 数模转换ADC08009应用

    #include <reg52.h> //头文件 #define uchar unsigned char //宏定义无符号字符型 #define uint unsigned int //宏 ...

  6. HTML(四)

    html表格 table常用标签 1.table标签:声明一个表格 2.tr标签:定义表格中的一行 3.td和th标签:定义一行中的一个单元格,td代表普通单元格,th表示表头单元格 table常用属 ...

  7. CDATA(不应由XML解析器进行解析的文本数据)、CDATA的使用场景

    1.1. CDATA: CDATA(Unparsed Character Data)指的是不应由XML解析器进行解析的文本数据. 因为XML解析器会将“<”(新元素的开始)和“&”(字符 ...

  8. 关于js键盘事件的例子

    JavaScript onkeydown 事件 用户按下一个键盘按键时会触发 onkeydown 事件.与 onkeypress事件不同的是,onkeydown 事件是响应任意键按下的处理(包括功能键 ...

  9. django之内置Admin

    本篇导航: 配置路由 定制Admin Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: 依赖APP: django.contrib.auth django ...

  10. 9、js扩展

    作用域是JavaScript最重要的概念之一,想要学好JavaScript就需要理解JavaScript作用域和作用域链的工作原理. 本片导航: js的作用域 作用域链(Scope Chain) 一. ...