一、题目

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

  • 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. 吴裕雄--天生自然 R语言开发学习:基本图形(续二)

    #---------------------------------------------------------------# # R in Action (2nd ed): Chapter 6 ...

  2. 深度学习之TensorFlow安装与初体验

    深度学习之TensorFlow安装与初体验 学习前 搞懂一些关系和概念 首先,搞清楚一个关系:深度学习的前身是人工神经网络,深度学习只是人工智能的一种,深层次的神经网络结构就是深度学习的模型,浅层次的 ...

  3. 转:zabbix 2.4.4 更换 logo

    zabbix 2.4.4 更换 logo 想把 zabbix 的 logo 改为自己公司的. 把更改过程做一下记录 先找到修改文件的目录 zabbix 版本 2.4.4 操作系统 linux 目录定位 ...

  4. Readings

    1984 ([英] 乔治·奥威尔) 这书看完我觉得这根本就是一本恐怖小说,当里面的内容正在和将要发生的时候你就不会觉得里面的描述有点搞笑了.不过看到后面有译者的补充内容说和其他国家的朋友讨论的时候, ...

  5. Python爬虫-百度模拟登录(二)

    上一篇-Python爬虫-百度模拟登录(一) 接上一篇的继续 参数 codestring codestring jxG9506c1811b44e2fd0220153643013f7e6b1898075 ...

  6. 用 Python 读写 Excel 表格

    Python 可以读写 Excel 表格吗? 当然可以. Python 下有很多类库可以做到, openpyxl 就是其中的佼佼者. openpyxl 的设计非常漂亮 ,你一定会喜欢它!不信请往下看: ...

  7. 【原创】(三)Linux进程调度器-进程切换

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  8. VirtualBox上使用kubeadm安装Kubernetes集群

    之前一直使用minikube练习,为了更贴近生产环境,使用VirtualBox搭建Kubernetes集群. 为了不是文章凌乱,把在搭建过程中遇到的问题及解决方法记在了另一篇文章:安装Kubernet ...

  9. 在高德地图上用svg.js绘制简单图形

    这段时间做的一个项目,需要在地图上绘制简单的图形.在学习高德地图JS API的过程中,发现高德地图提供的点.线等API并不能满足我的需求,还好它开放了自定义图层CustomLayer,官方说自定义图层 ...

  10. EventEmitter:从命令式 JavaScript class 到声明函数式的华丽转身

    新书终于截稿,今天稍有空闲,为大家奉献一篇关于 JavaScript 语言风格的文章,主角是函数声明式. 灵活的 JavaScript 及其 multiparadigm 相信"函数式&quo ...