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) - ...
随机推荐
- 你知道你对 JSON Web Token 的认识存在误解吗
1.前言 JSON Web Token (JWT) 其实目前已经广为软件开发者所熟知了,但是 JOSE (Javascript Object Signing and Encryption) 却鲜有人知 ...
- Caffe Ubuntu14.04 + CUDA 8 (支持GTX1080 1070等Pascal架构显卡)
1. 前言 本教程使用的系统是Ubuntu 14.04 LTS 64-bit,使用的CUDA版本为8. 理论上本教程支持Pascal架构显卡,如游戏卡GeForce GTX1070,GTX 1080, ...
- windows下pip的安装
安装地址:https://pypi.python.org/pypi/pip#downloads 下载完成后,找到文件并进行解压,找到下面路径. 打开cmd,cd到当前目录下,然后执行下面命令: pyt ...
- 892A. Greed#贪婪(优先队列priority_queue)
题目出处:http://codeforces.com/problemset/problem/892/A 题目大意:有一些可乐(不一定装满),问能不能把所有可乐装进两个可乐瓶中 #include< ...
- [CTS2019]氪金手游(容斥+树形背包DP)
降智好题.本蒟蒻VP时没想到怎么做被题面迷惑了,只会20分的“好”成绩.简直自闭了. 首先显然度为0的点是白给的,根据等比数列求和公式即可求得.然后考虑这个树如果是一颗外向树,就是每个点先父亲再自己. ...
- [单调队列]XKC's basketball team
XKC's basketball team 题意:给定一个序列,从每一个数后面比它大至少 \(m\) 的数中求出与它之间最大的距离.如果没有则为 \(-1\). 题解:从后向前维护一个递增的队列,从后 ...
- lnmp环境搭建:Centos7 + Nginx1.12.2 + Mysql-5.6.38 + PHP7.2.0
https://blog.csdn.net/ty_hf/article/details/50622888
- 三十六、www服务nginx介绍
一.Nginx介绍 ,相对于LAMP经典组合而言,LNMP是近几年来流行的组合.(linux+nginx+mysql+php) Nginx是一个开源www服务软件,是俄罗斯人开发的,本身是一款静态ww ...
- 吴裕雄--天生自然python学习笔记:python 建立 Firebase 数据库连接
Python 程序通过 python-firebase 包可以存取 Firebase 数据库. 使用 python-firebase 包 首先必须安装 python-firebase 包,安装方法如下 ...
- if necessary