【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2558字,纯文字阅读大概需要4分钟。
内容图文
![【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】](/upload/InfoBanner/zyjiaocheng/1322/ff4a6da4a33a4f07a05943c67189356a.jpg)
【225-Implement Stack using Queues(用队列实现栈操作)】
【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
代码下载【https://github.com/Wang-Jun-Chao】
原题
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
题目大意
使用队列实现栈操作
push(x) – 元素入栈
pop() – 元素出栈
top() – 取栈顶元素值
empty() – 判断栈是否为空
注意:
只能使用队列的标准操作,先进先出,求队列元素数,判断队列是否为空
由于编程语言原因,有些语言不支抚摩队列,可以使用链表或双向链表代替,但仅能使用标准的队列操作
你可以假设所有的操作都是合法的,即:当队列为空时不会有元素出栈和求栈顶元素的操作
解题思路
用两个队列来模拟一个栈
代码实现
算法实现类
import java.util.LinkedList;
import java.util.List;
publicclassMyStack {// 维持两个队列,其中总有一个队列为空,为pop和top操作准备private List<Integer> aList = new LinkedList<>();
private List<Integer> bList = new LinkedList<>();
// Push element x onto stack.publicvoidpush(int x) {
// 如果aList非空,就将x添加到aList中if (!aList.isEmpty()) {
aList.add(x);
}
// 否则总添加到bList中else {
bList.add(x);
}
}
// Removes the element on top of the stack.publicvoidpop() {
// 两个队列中至少有一个为空,将aList设置非空if (aList.isEmpty()) {
List<Integer> tmp = bList;
bList = aList;
aList = tmp;
}
// 除最后一个元素外都转移到bList中while (aList.size() > 1) {
bList.add(aList.remove(0));
}
// 删除最后一个元素(对应就是入栈的栈顶元素)
aList.clear();
}
// Get the top element.publicinttop() {
// 两个队列中至少有一个为空,将aList设置非空if (aList.isEmpty()) {
List<Integer> tmp = bList;
bList = aList;
aList = tmp;
}
// 除最后一个元素外都转移到bList中while (aList.size() > 1) {
bList.add(aList.remove(0));
}
bList.add(aList.get(0));
return aList.remove(0);
}
// Return whether the stack is empty.publicbooleanempty() {
return aList.isEmpty() && bList.isEmpty();
}
}
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/48084069】
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/derrantcm/article/details/48084069
内容总结
以上是互联网集市为您收集整理的【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】全部内容,希望文章能够帮你解决【LeetCode-面试算法经典-Java实现】【225-Implement Stack using Queues(用队列实现栈操作)】所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。