LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解
题目链接
https://leetcode-cn.com/problems/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
思路
使用两个栈来完成操作, 首先全部进入第一个栈,再全部进入第二个栈,用图来演示一下:
首先进入栈1;

然后出栈1,入栈2;
再出栈2。
由图可以知道,用两个栈即可完成队列的操作;
分为下面三种情况
- Stack_1空,Stack_2有元素,这时push()操作让Stack_1进行push();pop()操作,只需让Stack_2进行pop();peek()操作,也只需让Stack_2进行peek()就可以了;这时队列不为空;
- Stack_1不空,Stack_2空,这时push()操作让Stack_1进行push();pop()操作需要将Stack_1的所有元素进入Stack_2,Stack_2进行pop();peek()操作,也只需要将Stack_1的所有元素进入Stack_2,再让Stack_2进行peek()就可以了;这是队列不为空;
- Stack_1空,Stack_2也为空,push()操作让Stack_1进行push()即可,pop()和push()无法完成,队为空。
代码
import java.util.Stack;
class MyQueue {
//初始化栈1和栈2
private Stack<Integer> Stack_1;
private Stack<Integer> Stack_2;
/** Initialize your data structure here. */
public MyQueue() {
Stack_1 = new Stack<>();
Stack_2 = new Stack<>();
}
//进入第一个栈
/** Push element x to the back of queue. */
public void push(int x) {
Stack_1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
//如果栈2是空的
if(Stack_2.isEmpty()){
//将栈1的所有元素入栈2
while(!Stack_1.isEmpty()){
Stack_2.push(Stack_1.pop());
}
}
if (!Stack_2.isEmpty()) {
return Stack_2.pop();
}
throw new RuntimeException("MyQueue空了!");
}
/** Get the front element. */
public int peek() {
//如果栈2是空的
if(Stack_2.isEmpty()){
//将栈1的所有元素入栈2
while(!Stack_1.isEmpty()){
Stack_2.push(Stack_1.pop());
}
}
if (!Stack_2.isEmpty()) {
return Stack_2.peek();
}
throw new RuntimeException("MyQueue空了!");
}
/** Returns whether the queue is empty. */
public boolean empty() {
return Stack_1.isEmpty() && Stack_2.isEmpty();
}
}
欢迎关注
扫下方二维码即可关注:,微信公众号:code随笔
LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解的更多相关文章
- LeetCode 232:用栈实现队列 Implement Queue using Stacks
题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- leetcode刷题记录——栈和队列
题目 232.用栈实现队列 class MyQueue { private Stack<Integer> in = new Stack<>(); private Stack&l ...
- LeetCode232 Implement Queue using Stacks Java 题解
题目: Implement the following operations of a queue using stacks. push(x) -- Push element x to the bac ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 【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( ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
随机推荐
- java package 包 学习笔记
编译命令示例: javac -d . Main.java 注:带参数-d自动建立文件目录, 只使用javac 则需要手工创建目录 把 class文件打包 jar命令 jar cvf T.jar *; ...
- ZEOSDBO控件的安装及使用方法
步骤:1:下载最新版的ZEOSDBO,官网:http://sourceforge.net/projects/zeoslib/ 2:解压文件到文件安装目录下:C:\Program Files\Embar ...
- ThreadPoolExecutor自定义线程池
1.ThreadPoolExecutor创建线程池的构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long ...
- MySQL5.7源码安装
一.获取MySQL5.7.20源码安装包,并上传至服务器 MySQL官网下载地址:https://dev.mysql.com/downloads/mysql/ 下载版本:mysql-boost-5 ...
- 基于邻接表的长度为k的简单路径的求解
描述 一个连通图采用邻接表作为存储结构.设计一个算法,判断无向图中任意给定的两点是否存在一条长度为k的简单路径. 输入 多组数据,每组m+3数据行.第一行有两个数字n,m和k,代表有n个顶点,m条边和 ...
- iOS 10 新增plist文件属性
大概统计了一下需要加的一些字段列在下面: NSContactsUsageDescription -> 通讯录 NSMicrophoneUsageDescription -> 麦克风 NSP ...
- PAT甲级——1019 General Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- VB.Net 正则表达式测试器
VB.Net制作的正则表达式工具,查询结果可以导出到Excel. 界面截图: 软件下载 Regexp.rar
- 洛谷-P3919-可持久化数组
题目传送门 sol:在洛谷上看到一种dfs + 离线的方法,可以解决大部分可持久化问题.把依赖关系看成边,然后建树.这样本来要解决的多个版本只要在一个版本上进行修改就好了. 离线 + dfs #inc ...
- hibernate多表查询sql,以及所得对象的处理
String sql ="SELECT id FROM tea WHERE tea.name=? "; SQLQuery query = this.getSession().cre ...