LeetCode#232-Implement Queue using Stacks-用栈实现队列
一、题目
使用栈实现队列的下列操作:
- 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-用栈实现队列的更多相关文章
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (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 ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
- 232 Implement Queue using Stacks 用栈来实现队列
使用栈来实现队列的如下操作: push(x) -- 将一个元素放入队列的尾部.pop() -- 从队列首部移除元素.peek() -- 返回队列首部的元素.empty() -- 返回队列是否为空.注意 ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- LeetCode OJ:Implement Queue using Stacks(栈实现队列)
比较典型的一个题目,easy,不过可以有许多实现方式. 这里用的方式是每次pop完成之后再将stack2中的内容立即倒回stack1中.但是其他的实现也可以不是这样,可以是需要push的时候检查再,如 ...
随机推荐
- windows版 Sublime Text 2 快捷键
ucifr 翻译了 Sublime Text 2 快捷键 Mac版,用win系统的哥们表示伤不起啊~ 今天把windows版 Sublime Text 2 快捷键 整理了出来,与众兄弟们分享: Ctr ...
- idea(or maven) 未结束字符串字面值 非法的表达式开始
[ERROR] *.java:[38,27] 未结束的字符串字面值 [ERROR] *.java:[38,53] 需要 ';' [ERROR] *.java:[41,19] 需要 ')' [ERROR ...
- PHP导出excel文件的多种方式
1.第一种实现的方法 set_time_limit(0); //逐条导出数据 ob_end_clean(); header("Content-type: application/vnd.ms ...
- 极力推荐大佬的java项目的博客
此次说明:这是我从码云上拉取下来的java项目,仅供试验说明,不做任何获利渠道,若是发现有人拿此做其他用处,需像码云上这位大佬说明,特此感谢!!! 准备工作: 安装 maven,jdk1.8, ...
- PHP的ArrayAccess接口介绍
在 PHP5 中多了一系列新接口.在 HaoHappy 翻译的你可以了解到他们的应用.同时这些接口和一些实现的 Class 被归为 Standard PHP Library(SPL).在 PHP5 中 ...
- sql -- 获取连续签到的用户列表
签到表: 需求:统计连续签到的 用户 1.根据用户和日期分组 select user_name, sign_date from user_sign group by user_name, sign_d ...
- Spring事务Transactional和动态代理(二)-cglib动态代理
系列文章索引: Spring事务Transactional和动态代理(一)-JDK代理实现 Spring事务Transactional和动态代理(二)-cglib动态代理 Spring事务Transa ...
- JavaScript,ajax与springboot之间的传参方式
Springboot:@PostMapping("/mDelTest")public String mDelTest (@RequestBody List<String> ...
- 纯JS实现KeyboardNav(学习笔记)一
纯JS实现KeyboardNav(学习笔记)一 这篇博客只是自己的学习笔记,供日后复习所用,没有经过精心排版,也没有按逻辑编写 GitHub项目源码 预览地址 最终效果 KeyboardNav使用指南 ...
- 前端javascript知识(三)
函数记忆,判断是不是质数. 方法一: function isPrime1(n){ if(n<=3){return true} else{ for(var i=2;i<Math.sqrt(n ...