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. ubantu 执行sudo apt-get update 出现校验不符问题

    一直被这个问题困扰,今天安装mongodb时,看了别人博客暂时解决了,不知道会不会出什么问题. 直接打开软件更新器: 然后点击其他软件,去掉两个独立的多选项: 再执行一遍 sudo apt-get u ...

  2. Yahoo Programming Contest 2019.D.Ears(DP)

    题目链接 菜爆了啊QAQ 记起点为\(S\),终点为\(T\),走过的最靠左的点是\(L\),最靠右的点是\(R\). 那么坐标轴被分成了五段: \(0\sim L-1\):经过\(0\)次: \(L ...

  3. Codeforces.666E.Forensic Examination(广义后缀自动机 线段树合并)

    题目链接 \(Description\) 给定串\(S\)和\(m\)个串\(T_i\).\(Q\)次询问,每次询问\(l,r,p_l,p_r\),求\(S[p_l\sim p_r]\)在\(T_l\ ...

  4. cf 443

    题目链接 A,对于每一位可以暴力输入,看输出是什么,然后就有2x2中对应方式,然后可以用3次运算搞了,好像网上在悬赏最多只用2次搞出来的. B,这个题可以先处理每个串内部的情况,再处理连接处的情况,代 ...

  5. mysql的基本查询(等于,不等于,between...and...,)

    单个字段多个字段查询 查询员工姓名 *注:在SQL语句中不区分大小写:SQL语句以“:”分号结束 select ename from emp; 注:select询句后面跟的是字段名称,select是关 ...

  6. Alpha冲刺——序言篇(任务与计划)

    所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺--序言篇 团队名称 待就业六人组 一.代码规范 详见项目在线文档:项目代码规范 二.本次冲刺任务与计划 任务 内容 时间 第一天 ...

  7. 11-8 定时器this

    定时器this问题 var t=setInterval(function(){ console.log(this) },1000) 这里面的this是window Person.prototype={ ...

  8. db2 reorg runstats rebind具体操作

    db2 reorg runstats rebind具体操作 #reorg table db2 -x "select 'reorg table '||rtrim(tabschema)||'.' ...

  9. 深入理解javascript构造函数和原型对象

    ---恢复内容开始--- 对象,是javascript中非常重要的一个梗,是否能透彻的理解它直接关系到你对整个javascript体系的基础理解,说白了,javascript就是一群对象在搅..(哔! ...

  10. macOS Sierra WiFi connecting problem

    吐槽一下,苹果的质量管控越来越差了. macOS Sierra有时突然或升级后会遇到wifi不停重连连不上问题,现象为不停地连接wifi. 网上有人说删除 /Library/Preferences/S ...