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) - ...
随机推荐
- mysql自定义函数初始化数据:init_data()
DELIMITER $$ USE `local_hnyz`$$ DROP FUNCTION IF EXISTS `init_data`$$ CREATE DEFINER=`root`@`localho ...
- windows系统下的渗透测试神器 -pentestbox
Pentestbox介绍 PentestBox官网:https://pentestbox.org/zh/ 这是一个运行在windows环境下的终端,集成了绝大部分渗透测试所需要的环境 如python2 ...
- eclipse优化自动补全(解决空格、分号等上屏问题)
第一步:打开"Window"——>"Show View"——>"Other"——>找到"Plug-in Deve ...
- Linux系统国内镜像站点
一,更换说明 第一步 备份 如centos, mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup ...
- 爱心Java for循环实现
public class x { public static void main(String[] args) { for (int i = 0, k = 0; i < 14; i++) { i ...
- 2. Unconstrained Optimization(2th)
2.1 Basic Results on the Existence of Optimizers 2.1. DefinitionLet $f:U->\mathbb{R}$ be a functi ...
- logback日志大量写磁盘导致微服务不能正常响应的解决方案
最近几天,遇到一个莫名其妙的问题,每天几乎同一时段微服务自己跑着跑着就假死了,过几个小时就又自动恢复了. 通过对定时任务.网卡.内存.磁盘.业务日志的排查分析,只有磁盘的IO在假死前一段时间偏高,经查 ...
- confidence intervals and precision|The One-Mean z-Interval Procedure|When to Use the One-Mean z-Interval Procedure
Confidence Intervals for One Population Mean When σ Is Known Obtaining Confidence Intervals for a Po ...
- Python接口自动化测试-下载文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : shenqiang ''' 注意:定义类的时候,内部方法之间的互调 步骤: 1.按照 ...
- 16)用了session会话技术
为什么用session会话技术? 因为假如你进入后台,不可能随意进入,即使你的验证通过了,那么还需要一个变量来存一个标志,假如标志的值是yes,那么我们可以直接进入后台的首页,无需验证,但是,标志是n ...