LeetCode 225题用队列实现栈(Implement Stack using Queues) Java语言求解
链接
https://leetcode-cn.com/problems/implement-stack-using-queues/
思路
首先演示push()操作;
将元素依次进入队1,进入时用top元素保存当前进入的元素;
如下图:
push操作的演示
然后演示pop()操作;
先将除队1中的最后一个元素出队并进入队2,入队2时用top存储入队元素;
再将队列1和队列2进行互换即可。
如下图:
pop操作的演示
代码
import java.util.LinkedList;
import java.util.Queue;
class MyStack {
private Queue<Integer> queue_1 = new LinkedList<>();
private Queue<Integer> queue_2 = new LinkedList<>();
private int top;
/** Initialize your data structure here. */
public MyStack() {
}
/** Push element x onto stack. */
public void push(int x) {
queue_1.add(x);
top = x;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
if(empty()){
throw new RuntimeException("MyStack空了!");
}
while(queue_1.size() > 1){
top = queue_1.remove();
queue_2.add(top);
}
int last_ele = queue_1.remove();
Queue<Integer> queue_temp = queue_1;
queue_1 = queue_2;
queue_2 = queue_temp;
return last_ele;
}
/** Get the top element. */
public int top() {
if(empty()){
throw new RuntimeException("MyStack空了!");
}
return top;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue_1.isEmpty() && queue_2.isEmpty() ;
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
欢迎关注
扫下方二维码即可关注:,微信公众号:code随笔
LeetCode 225题用队列实现栈(Implement Stack using Queues) Java语言求解的更多相关文章
- LeetCode 225:用队列实现栈 Implement Stack using Queues
题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...
- [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 两个队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 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) - ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
随机推荐
- 4)PHP命名规则,传值方式
(1)命名规则: 包括变量名,类名,接口名函数名等等 ①基本规则: 只能使用小写字母,下划线或者数字 数字不能开头 不能跟环境和系统关键字重复(比如,if,else,function) ② 驼峰式 ...
- 吴裕雄--天生自然TensorFlow高层封装:Keras-多输入输出
# 1. 数据预处理. import keras from keras.models import Model from keras.datasets import mnist from keras. ...
- Java之关键字abstract(抽象类与抽象方法)
/* * abstract关键字的使用 * 1.abstract:抽象的 * 2.abstract可以用来修饰的结构:类.方法 * * 3. abstract修饰类:抽象类 * > 此类不能实例 ...
- 解决安装 .net framework 发生 extracting files error 问题
VMware虚拟机环境 WIn7 SP1下离线安装 .net framework 4.5.2 遇到 extracting files error 错误,开始以为是文件损坏,结果换 4.7, 4.8 都 ...
- mybatis处理LIKE模糊查询字符串拼接
-- 最佳实践 <select id="getSealByMap" parameterType="map" resultType="map&qu ...
- Normally Distributed|
6.1Introducing Normally Distributed Variables Why the word “normal”? Because, in the last half of th ...
- 对xgboost中dump_model生成的booster进行解析
xgboost原生包中有一个dump_model方法,这个方法能帮助我们看到基分类器的决策树如何选择特征进行分裂节点的,使用的基分类器有两个特点: 二叉树: 特征可以重复选择,来切分当前节点所含的数据 ...
- python学习笔记(18)字典和json 的区别 和转换
字典和json 的区别 和转换 前言:字典和json非常像.接下来比较一下两者的异同 先看一下字典的写法: a = {'a':'1', 'b':'2', 'c':'3' } 再看一下json的写法: ...
- @EnableWebMvc WebMvcConfigurer CorsConfig
package me.zhengjie.core.config; import org.springframework.context.annotation.Configuration; import ...
- 2019牛客多校(第十场)F Popping Balloons —— 线段树+枚举
https://ac.nowcoder.com/acm/contest/890/F 题意:二维平面中有n个气球,你可以横着社三法子弹,竖着射三发子弹,且横着子弹的关系是y,y+r,y+2*r,竖着是x ...