一、题目

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

  • 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;
}
}

最新文章

  1. arcgis api for js入门开发系列七图层控制(含源代码)
  2. Scala override
  3. Oracle date 和 timestamp 区别
  4. mysql 段错误 (core dumped)
  5. open/write/read
  6. ASP.NET的六种验证控件的使用
  7. Yii中CDbCriteria常用方法
  8. 解决android studio 创建新项目后假死
  9. 全栈JavaScript路(八)得知 CDATASection 种类 节点
  10. libusb开发者指南(转)
  11. 基于NIOS-II的示波器:PART3 初步功能实现
  12. [Bootstrap 源码]——bootstrap源码之初始化
  13. Java看书学习笔记
  14. Java编程的逻辑 (93) - 函数式数据处理 (下)
  15. 在Windows10中运行debug程序
  16. 【创建模式】--Singleton
  17. JS实现简单的运行代码 & 侧边广告
  18. MD5加密和sha加密
  19. 【Revit API】FamilyInstance、FamilySymbol、Family的寻找关系
  20. The minimum required Cuda capability is 3.7.

热门文章

  1. IPFS私有网络搭建总结
  2. 上传第三方jar包到nexus
  3. python基础 生成器 迭代器
  4. CentOS使用epel安装不同版本php-fpm
  5. Dangerous query method called with non-attribute argument(s)
  6. Ansible(一) Try it - 枯鱼的博客
  7. Nginx设置目录浏览并配置验证
  8. Flutter调研(2)-Flutter从安装到运行成功的一些坑
  9. 做直播能有多赚钱,Python告诉你
  10. sql05