博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode算法题-Implement Queue Using Stacks(Java实现)
阅读量:6857 次
发布时间:2019-06-26

本文共 4537 字,大约阅读时间需要 15 分钟。

这是悦乐书的第195次更新,第201篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232)。使用栈实现队列的以下操作。

push(x) - 将元素x推送到队列的后面。

pop() - 从队列前面删除元素。

peek() - 获取前面的元素。

empty() - 返回队列是否为空。

例如:

MyQueue queue = new MyQueue();

queue.push(1);

queue.push(2);

queue.peek(); //返回1

queue.pop(); //返回1

queue.empty(); //返回false

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

队列的特性是先进先出,而栈的特性是先进后去,在使用栈进行队列的出队列和队顶操作时,需要借助另外一个栈来进行反转然后再还原,而入队列的操作还是无需特殊处理。

class MyQueue {    private Stack
stack; /** Initialize your data structure here. */ public MyQueue() { stack = new Stack
(); } /** Push element x to the back of queue. */ public void push(int x) { stack.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { Stack
temp = new Stack
(); while (!stack.isEmpty()) { temp.push(stack.pop()); } int tem = temp.pop(); while (!temp.isEmpty()) { stack.push(temp.pop()); } return tem; } /** Get the front element. */ public int peek() { Stack
temp = new Stack
(); while (!stack.isEmpty()) { temp.push(stack.pop()); } int tem = temp.peek(); while (!temp.isEmpty()) { stack.push(temp.pop()); } return tem; } /** Returns whether the queue is empty. */ public boolean empty() { return stack.isEmpty(); }}

03 第二种解法

此解法和上面的第一种解法正好相反,是在入队列的时候,借助另外一个栈来进行反转操作,而出队列和获取队列顶的操作可以直接使用栈的方法,无需特殊处理。

class MyQueue2 {    Stack
stack; /** Initialize your data structure here. */ public MyQueue2() { stack = new Stack
(); } /** Push element x to the back of queue. */ public void push(int x) { Stack
temp=new Stack<>(); while(!stack.isEmpty()){ temp.push(stack.pop()); } temp.push(x); while(!temp.isEmpty()){ stack.push(temp.pop()); } } /** Removes the element from in front of queue and returns that element. */ public int pop() { return stack.pop(); } /** Get the front element. */ public int peek() { int a = stack.pop(); stack.push(a); return a; } /** Returns whether the queue is empty. */ public boolean empty() { return stack.isEmpty(); }}

04 第三种解法

此解法使用两个栈s1、s2来实现队列的相关操作。

入队列时,如果s2不为空,那么先把s2中的元素pop出来在push进s1,然后才去将当前要插入的数据push进s1。

出队列时,如果s2为空,即此前没有进行出队列操作或者获取队列顶的操作,那么就需要将s1反转,即将s1的元素pop出来,然后push进s2中,此时再返回s2的pop操作即可。如果s2不为空,即说明上一次操作不是入队列,而是出队列或获取队列顶的操作,直接返回s2的pop操作即可。

获取队列顶时,和入队列操作时的判断一致,只不过最后返回s2的peek操作即可。

class MyQueue3 {    private Stack
s1; private Stack
s2; /** Initialize your data structure here. */ public MyQueue3() { s1 = new Stack
(); s2 = new Stack
(); } /** Push element x to the back of queue. */ public void push(int x) { while(!s2.empty()) { s1.push(s2.pop()); } s1.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { if (s2.empty()) { while (!s1.empty()) { s2.push(s1.pop()); } } return s2.pop(); } /** Get the front element. */ public int peek() { if (s2.empty()) { while (!s1.empty()) { s2.push(s1.pop()); } } return s2.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { return (s1.empty() && s2.empty()); }}

05 第四种解法

在入队列时,始终往第一位插入元素,而其他的出队列、获取队列的顶、判空这些操作都可以直接使用栈的方法,而无需重新实现。

class MyQueue4 {    private Stack
stack; /** Initialize your data structure here. */ public MyQueue4() { stack = new Stack
(); } /** Push element x to the back of queue. */ public void push(int x) { stack.add(0, x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { return stack.pop(); } /** Get the front element. */ public int peek() { return stack.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { return stack.isEmpty(); }}

06 小结

算法专题目前已连续日更超过一个月,算法题文章57+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

转载于:https://www.cnblogs.com/xiaochuan94/p/10087508.html

你可能感兴趣的文章
搭建nginx流媒体服务器(支持HLS)
查看>>
struts2上传文件大小受限问题
查看>>
dao使用JdbcTemplate(注入过程)视频学习
查看>>
无刷新URL 更新
查看>>
狮入羊口
查看>>
HDU 1421 搬寝室[DP]
查看>>
二层设备与三层设备的区别--总结
查看>>
ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)
查看>>
JS操作css样式用法
查看>>
怎样使用 CCache 进行 cocos2d-x 编译加速
查看>>
Thymeleaf 3.0 专题
查看>>
Spring下的@Inject、@Autowired、@Resource注解区别(转)
查看>>
View的setTag()与getTag()方法使用
查看>>
UML中类结构图示例
查看>>
03-hibernate注解-关系映射级别注解-一对一
查看>>
EasyUI combotree的使用
查看>>
C#网络编程二:SOCKET编程
查看>>
【多媒体封装格式详解】--- AAC ADTS格式分析
查看>>
联想IDEAPAD 320C-15笔记本显卡驱动问题
查看>>
ES6简介
查看>>