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() -- 返回栈是否为空 注意: 你必须使用 ...
随机推荐
- 绿洲作业第二周 - 周二music work 音乐
Please kindly find the music work from Ms. Sophie. 1.请跟随附件中老师录制的视频进行学习和练习.(附件有带拼音的乐谱供KS1和外国学生使用) htt ...
- mybatis处理LIKE模糊查询字符串拼接
-- 最佳实践 <select id="getSealByMap" parameterType="map" resultType="map&qu ...
- 49),PHP, 重写
- winfrom控件圆角
刚好用到这个功能,看了好些例子.我就不明白,简单的一个事,一些文章里的代码写的那个长啊,还让人看么. 精简后,就其实一点,只要有paint事件的组件,都可画圆角,没有的外面套一个panel就行了. u ...
- aclocal-1.13: command not found
原因: 将编译好的工程拷贝到系统版本不一样的系统中,再进行编译会出现此类问题. 解决方法: yum install automake autoconf yum install libtool auto ...
- QuickSort(快速排序)原理及C++代码实现
快速排序可以说是最重要的排序,其中延伸的思想和技巧非常值得我们学习. 快速排序也使用了分治的思想,原理如下: 分解:数组A[p..r]被划分为两个(可能为空)子数组A[p..q-1]和A[q+1..r ...
- [LC] 513. Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 ...
- [LC] 692. Top K Frequent Words
Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...
- 吴裕雄--天生自然python学习笔记:python OpenCV 基本绘图
Open CV 提供了绘制直线.圆形.矩形等基本绘 图的功能 . Open CV 画直线的语法为: 在画布上添加文字的语法为 : 用 Open CV 绘制基本图形 以 OpenCV 基本绘图绘制各种图 ...
- sklearn包源码分析(一)--neighbors
python如何查看内置函数的用法及其源码? 在anaconda的安装目录下,有一块会放着我们安装的所有包,在里面可以找到所有的包 找到scikit learn包,进入 这里面又有了多个子包,每个子包 ...