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() -- 返回栈是否为空 注意: 你必须使用 ...
随机推荐
- 对于 C语言的扩展和JAVA的重载理解
哎,又被学长看成笨蛋了 ,先前学习java,自己真是什么都要忘了,弄得自己连java最重要的概念--重载,都不知道是啥,还厚着脸皮和学长说 是函数名字一样 ,但是就是函数里面的参数和参数类型不一 ...
- RDD(十)——案例实操
需求: 数据结构:时间戳,省份,城市,用户,广告,中间字段使用空格分割. 样本如下: 1516609143867 6 7 64 16 1516609143869 9 4 75 18 151660914 ...
- eclipse利用sql语句对Oracle数据库进行操作
对Oracle数据库执行操作的sql语句中表名和列名都需用英文双引号("")括起来. 注(\为转义符) 1.插入数据 sql = "insert into \" ...
- Uber坚持不盈利,葫芦里到底卖的是什么药?
近日,据媒体报道在美国科罗拉多州阿斯彭举办的<财富>科技头脑风暴大会上,Uber CEO达拉·科斯罗萨西表示,Uber无需在2019年下半年上市计划实施前保持盈利状态. 首先要明确一点的是 ...
- visual studio code快捷键(mac)
常用快捷键 命令 命令描述 备注 ctl+j 多行文字变成一行 打开首选项->键盘快捷方式->搜索 joinLines: 然后设置你的快捷键再回车即可.
- pom配置项目build jar 包含源码的配置
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...
- 学习4412开发板gdb和gdbserver的调试
因为有很多的小伙伴是从单片机转过来的,用惯了单片机上的JLINK调试程序,换到Linux上非常的不习惯.确实,如果能设置断点,单步调试,查看变量,那确实是太爽了,那么在我们的Linux可以做到吗,答案 ...
- linux下java调用C
linux下java调用C 分类: linux2012-05-22 09:12 1529人阅读 评论(0) 收藏 举报 javalinuxmakefilegccclasscommand 下面是在ubu ...
- jsp页面链接文件
Myeclipse中jsp页面链接css文件不生效问题 jsp页面在外链css文件后,样式表不生效,查了很多方法,都说是路径问题改了很多次也不行. 例如改为: <link rel="s ...
- Cisco路由器配置基本命令
特权模式:enable Router1 # show running-config Router1 # show ip route Router # show ip interface brief S ...