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. 移动端滑屏全应用【四】移动端动画贞动画函数mTween封装

    首先此函数是基于大家都知道的Tween动画算法的,在此基础上使用了三中讲到的兼容版动画贞,可以使动画变得更流畅. 1. 首先要记得引入Tween.js 2. 引入mTween.js 3. 调用 * m ...

  2. How to check for null/empty/whitespace values with a single test?

    SELECT column_name FROM table_name WHERE TRIM(column_name) IS NULL

  3. VMware5.5-存储

    存储 存储类型 VMFS(vmvare公司提供) NFS 本地存储 添加主机硬盘 扩展现有的磁盘或者添加新的硬盘 添加完成后点击全部重新扫描 添加存储器. 网络存储 网络存储的运用大大提高了虚机话的便 ...

  4. 字节跳动冬令营网络赛 D.The Easiest One(贪心 数位DP)

    题目链接 \(x:\ 11010011\) \(y:\ 10011110\) (下标是从高位往低位,依次是\(1,2,...,n\)) 比如对于这两个数,先找到最高的满足\(x\)是\(0\),\(y ...

  5. 2018.12.1 Test

    目录 2018.12.1 Test A 串string(思路) B 变量variable(最小割ISAP) C 取石子stone(思路 博弈) 考试代码 B C 2018.12.1 Test 题目为2 ...

  6. lambda表达式 匿名函数

    lambda函数是一种快速定义单行最小函数的方法,是从Lisp借鉴而来的,可以用在任何需要函数的地方. 基础 lambda语句中,冒号前是参数,可以有多个,用逗号分割:冒号右边是返回值. lambda ...

  7. 回文检测 [USACO Training Section 1.3]

    题目描述 据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文.你的工作就是去寻找这些牛制造的奇观(最棒的回文). 在寻找回文时不用理睬那些标点符号.空格( ...

  8. 3ds max学习笔记(十三)-- (锥化,扭曲,晶格)

    锥化: 新建一个长方体,在修改器列表中选择--锥化: 参数同弯曲有些类似 参数里: 数量:控制上界面的缩放 曲线:橘黄色的线(但模型未发生变化,此时,可以回到物体身上,调整对应方向上的分段数) 组(有 ...

  9. db2和oracle字段类型对比

    在网上找到一片关于oracle与DB2数据类型的比较,特记下来. Oracle DB2/400 Oracle数据类型 注意事项 DB2 UDB数据类型 注意事项 DATE DATETIMETIMEST ...

  10. db2 reorg runstats rebind具体操作

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