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. 有关python 函数参数

    # def foo(x):# print(x)### foo(1)# foo('a')# foo({'a':2}) #形参与实参:# def foo(x,y): #x=1,y=2# return x+ ...

  2. BZOJ.4399.魔法少女LJJ(线段树合并)

    BZOJ 注意\(c\leq7\)→_→ 然后就是裸的权值线段树+线段树合并了. 对于取\(\max/\min\)操作可以直接区间修改清空超出范围的值,然后更新到对应位置上就行了(比如对\(v\)取\ ...

  3. 洛谷.3808/3796.[模板]AC自动机

    题目链接:简单版,增强版 简单版: #include <cstdio> #include <cstring> const int N=1e6+5,S=26; char s[N] ...

  4. 将一个C++的AES加密算法(有向量的)翻译成C#

    /****************************************************************************** Copyright (c) 2012-2 ...

  5. mysql安装5.7.20版本

    安 装 命 令:mysqld install 初始化命令:mysqld –initialize 参考这个链接 http://blog.csdn.net/wlc57627451/article/deta ...

  6. 【分治-前缀积后缀积】JS Window @2018acm徐州邀请赛G

    问题 G: JS Window 时间限制: 2 Sec  内存限制: 512 MB 题目描述 JSZKC has an array A of N integers. More over, he has ...

  7. EF Core中执行Sql语句查询操作之FromSql,ExecuteSqlCommand,SqlQuery

    一.目前EF Core的版本为V2.1 相比较EF Core v1.0 目前已经增加了不少功能. EF Core除了常用的增删改模型操作,Sql语句在不少项目中是不能避免的. 在EF Core中上下文 ...

  8. 对类的理解(c++)

    介绍目录: 1.类成员 1.1 成员函数 1.2 构造函数 1.2.1 对构造函数的理解 1.2.2成员初始化列表 1.2.3必须使用成员初始化列表的几种情况 1.2.4对于拷贝构造函数的参数是一个引 ...

  9. AssemblyInfo.cs文件详解

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq395537505/article/details/49661555 一.前言 .net工程的Pr ...

  10. MyBatis架构与源码分析<资料收集>

    1.架构与源码分析 :https://www.cnblogs.com/luoxn28/p/6417892.html .https://www.cnblogs.com/wangdaijun/p/5296 ...