一、题目

使用栈实现队列的下列操作:

  • push(x) -- 将一个元素放入队列的尾部。
  • pop() -- 从队列首部移除元素。
  • peek() -- 返回队列首部的元素。
  • empty() -- 返回队列是否为空。

示例:

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

说明:

  • 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。

二、题解

  • 解法1:两个栈

队列是先进先出,栈是先进后出。所以可以用两个栈,新元素压入栈的时候,先将栈中的元素弹出,放到另一个栈中,把新元素压入到栈的底部,再把另一个栈的元素弹出,放回原栈中。此时,栈顶元素就是出队时要先出的队首元素。

这里用的是数组表示队列,空间复杂度O(n),时间复杂度分 push 和 pop ,前者是O(n),后者是O(1)。

class MyQueue {
/**
* Initialize your data structure here.
*/
function __construct() {
$this->stack1 = [];
$this->stack2 = [];
} /**
* Push element x to the back of queue.
* @param Integer $x
* @return NULL
*/
function push($x) {
while (!$this->empty()) {
$this->stack2[] = array_pop($this->stack1);
}
$this->stack1[] = $x;
while (!empty($this->stack2)) {
$this->stack1[] = array_pop($this->stack2);
}
} /**
* Removes the element from in front of queue and returns that element.
* @return Integer
*/
function pop() {
return array_pop($this->stack1);
} /**
* Get the front element.
* @return Integer
*/
function peek() {
return end($this->stack1);
} /**
* Returns whether the queue is empty.
* @return Boolean
*/
function empty() {
return empty($this->stack1) ? true :false;
}
}
  • 解法2:两个栈

使用两个栈,一个栈(stack1)仅负责入栈,一个栈(stack2)仅负责出栈。有新元素入队时,直接将元素压入 stack1 即可。但当出队时,需要判断 stack2 是否为空,如果为空,将 stack1 的元素依次出栈,压入 stack2 中,随后从 stack2 弹出,即为出队。但当 stack2 不为空时,仍然直接从 stack2 出栈即可,知道 stack2 为空时,才可将 stack1 的元素拿出来放入 stack2 中。

这里用的是数组表示队列,空间复杂度O(n),时间复杂度分 push 和 pop ,前者是O(n),后者是O(1)。

class MyQueue2 {
/**
* Initialize your data structure here.
*/
function __construct() {
$this->stack1 = [];
$this->stack2 = [];
} /**
* Push element x to the back of queue.
* @param Integer $x
* @return NULL
*/
function push($x) {
$this->stack1[] = $x;
} /**
* Removes the element from in front of queue and returns that element.
* @return Integer
*/
function pop() {
if (empty($this->stack2)) {
while (!empty($this->stack1)) {
$this->stack2[] = array_pop($this->stack1);
}
}
return array_pop($this->stack2);
} /**
* Get the front element.
* @return Integer
*/
function peek() {
if (empty($this->stack2)) {
while (!empty($this->stack1)) {
$this->stack2[] = array_pop($this->stack1);
}
}
return end($this->stack2);
} /**
* Returns whether the queue is empty.
* @return Boolean
*/
function empty() {
return empty($this->stack1) && empty($this->stack2) ? true : false;
}
}

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 232 Implement Queue using Stacks 和 231 Power of Two

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

  3. LeetCode 232 Implement Queue using Stacks

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

  4. (easy)LeetCode 232.Implement Queue using Stacks

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

  5. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

  6. Leetcode 232 Implement Queue using Stacks STL

    本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...

  7. LeetCode 232 Implement Queue using Stacks 两个栈实现队列

    class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...

  8. 232 Implement Queue using Stacks 用栈来实现队列

    使用栈来实现队列的如下操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空.注意 ...

  9. Java for LeetCode 232 Implement Queue using Stacks

    Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...

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

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

随机推荐

  1. 两步解决maven plugins 插件下载慢 !下载报红的问题!

    两步解决maven plugins 插件下载慢 !下载报红的问题! 1.找到你解压的maven安装路径下的conf   编辑settings 2.添加如下   使用阿里的 <mirror> ...

  2. Python 列表[::-1]翻转

    b = a[i:j:s]: i为起始索引(缺省为0), j为结束索引(不包括,缺省为len(a)), s为步进(缺省为1). 所以a[i:j:1]相当于a[i:j]. 当s<0时: i缺省时,默 ...

  3. linux公社大量免费的在线android资料

    2011年linux数据库的android在线分享 linux公社:开源公社             本文撰写:杨凯专属频道 下载如需密码,详见博客案例:点击我去查看密码 2011年9月12日 21: ...

  4. 在CodaLab上提交MURA竞赛的结果

    What is MURA? MURA (musculoskeletal radiographs) is a large dataset of bone X-rays. Algorithms are t ...

  5. 致歉Note7用户后,三星要还给世界下一个机皇

    ​ 1月23日,三星电子在韩国首尔公布了Note7事件原因调查结果.此次认定过程精密而繁琐,最终结果发布距离三星宣布全球召回Note7已经有3个月. 相比事发之初各路信息甚嚣尘上,现在虽然还不能说已经 ...

  6. Simplifying Conditional Expressions(简化条件表达式)

    1.Decompose Conditional(分解条件表达式) 2.Consolidate Conditional Expressions(合并条件表达式) 3.Consolidate Duplic ...

  7. AndroidStudio自动导入包

    IntelliJ IDEA可以自动优化导入包,但是有多个同名的类位于不同的包时,需要自己手动使用Alt + Enter进行导入. Settings→Editor→General→Auto Import ...

  8. 机器学习- RNN以及LSTM的原理分析

    概述 RNN是递归神经网络,它提供了一种解决深度学习的另一个思路,那就是每一步的输出不仅仅跟当前这一步的输入有关,而且还跟前面和后面的输入输出有关,尤其是在一些NLP的应用中,经常会用到,例如在NLP ...

  9. 从0开发3D引擎(十一):使用领域驱动设计,从最小3D程序中提炼引擎(第二部分)

    目录 上一篇博文 本文流程 回顾上文 解释基本的操作 开始实现 准备 建立代码的文件夹结构,约定模块文件的命名规则 模块文件的命名原则 一级和二级文件夹 api_layer的文件夹 applicati ...

  10. 使用Pods中使用Swift和Objective-C混编-编译不通过的原因

    iOS开发#使用Pods中使用Swift和Objective-C混编-编译不通过的原因-ld: symbol(s) not found for architecture arm64 问题基本描述 在P ...