LeetCode算法题-Implement Stack Using Queues
这是悦乐书的第193次更新,第198篇原创
01 看题和准备
今天介绍的是LeetCode算法题中Easy级别的第54题(顺位题号是225)。使用队列实现栈的以下操作:
push(x) - 将元素x推入栈。
pop() - 删除栈顶部的元素。
top() - 获取顶部元素。
empty() - 返回栈是否为空。
例如:
MyStack stack = new MyStack();
stack.push(1);
stack.push(2);
stack.top(); //返回2
stack.pop(); //返回2
stack.empty(); //返回false
本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。
02 第一种解法
借助ArrayList。进栈利用list的add方法。出栈时先获取list最后一位元素存起来,然后再删除最后一位元素,再返回先前存起来的最后一位元素。获取栈顶时,返回list最后一位元素即可。判空可以直接利用list的isEmpty方法。
class MyStack {
List<Integer> list = null;
/** Initialize your data structure here. */
public MyStack() {
list = new ArrayList<Integer>();
}
/** Push element x onto stack. */
public void push(int x) {
list.add(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
int top = list.get(list.size()-1);
list.remove(list.size()-1);
return top;
}
/** Get the top element. */
public int top() {
return list.get(list.size()-1);
}
/** Returns whether the stack is empty. */
public boolean empty() {
return list.isEmpty();
}
}
03 第二种解法
利用队列。
进栈直接使用queue的add方法或者offer方法。
出栈时,因为队列是先进先出的特性,所以需要先把队列里的元素poll出来再add进去,但是此操作只能进行queue的size-1次,比如:进栈顺序是1->2->3,进行一次出队列再进队列操作后,2->3->1,再进行一次出队列再进队列的操作,3->1->2,如果你再操作一次就还原了,所以只能操作queue的size-1次。最后再使用队列的poll方法,实现出栈。
栈顶,和出栈的思路一样,不过在把队列里的元素poll出来再add进去size-1次后,先要将顶获取到,可以直接使用队列的peek方法,然后再来一次poll出来再add进去,就把队列元素的顺序还原了。方便下次操作。
判空可以直接使用队列自身的isEmpty方法。
class MyStack2 {
Queue<Integer> queue = null;
int size ;
/** Initialize your data structure here. */
public MyStack2() {
queue = new LinkedList<Integer>();
size = 0;
}
/** Push element x onto stack. */
public void push(int x) {
queue.add(x);
size++;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
for (int i=1; i<size; i++) {
queue.add(queue.poll());
}
size--;
return queue.poll();
}
/** Get the top element. */
public int top() {
for (int i=1; i<size; i++) {
queue.add(queue.poll());
}
int res = queue.peek();
queue.add(queue.poll());
return res;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
04 第三种解法
还是使用队列。不过此解法除了入栈重新实现外,其他的出栈、栈顶、判空都是使用了队列的方法,所以重点讲下入栈即可。
入栈时,首先利用队列的add方法,然后利用循环,把队列里的元素poll出来再add进去,此循环只执行队列的size-1次,执行size次会还原。
class MyStack3 {
Queue<Integer> queue = null;
/** Initialize your data structure here. */
public MyStack3() {
queue = new LinkedList<Integer>();
}
/** Push element x onto stack. */
public void push(int x) {
queue.add(x);
for (int i=0; i<queue.size()-1; i++) {
queue.add(queue.poll());
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
}
/** Get the top element. */
public int top() {
return queue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
05 小结
算法专题目前已连续日更超过一个月,算法题文章54+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。
以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!
LeetCode算法题-Implement Stack Using Queues的更多相关文章
- LeetCode算法题-Implement Queue Using Stacks(Java实现)
这是悦乐书的第195次更新,第201篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232).使用栈实现队列的以下操作. push(x) - 将元素x推 ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- 【算法】LeetCode算法题-Implement strStr
这是悦乐书的第151次更新,第153篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第10题(顺位题号是28).给定两个任意字符串haystack.needle,返回hay ...
- 【LeetCode】225. Implement Stack using Queues
题目: Implement the following operations of a stack using queues. push(x) -- Push element x onto stack ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- LeetCode OJ:Implement Stack using Queues(队列实现栈)
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 【LeetCode】225. Implement Stack using Queues 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【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( ...
随机推荐
- golang使用chrome headless获取网页内容
如今动态渲染的页面越来越多,爬虫们或多或少都需要用到headless browser来渲染待爬取的页面. 而最近广泛使用的headless browser解决方案PhantomJS已经宣布不再继续维护 ...
- .Net NPOI 根据excel模板导出excel、直接生成excel
一.根据Excel模板导出excel 1.导入NPOI.dll 2.DAL中添加类ExportExcel.cs using NPOI.SS.UserModel; using System; usin ...
- 从零开始学安全(十六)● Linux vim命令
游标控制 h 游标向左移 j 游标向下移 k 游标向上移 l (or spacebar) 游标向右移 w 向前移动一个单词 b 向后移动一个单词 e 向前移动一个单词,且游标指向单词的末尾 ( 移到当 ...
- Android Studio 使用Menu
首先在res目录下创建一个文件夹名字随意 在对创建的文件夹下在创建一个菜单 名字随意 参看布局 可以看到你的菜单 可以选择添加是么样的菜单 接着要到主活动中重写 onCreateOptionsMenu ...
- 如何在 Mac上 安裝 .NET Core 2.1 ?
一.前言 Free. Cross-platform. Open source. A developer platform for building all your apps. --- .net co ...
- 49.Linux-wpa_cli使用之WIFI开启,扫描热点,连接热点,断开热点,WIFI关闭(49)
本章学习内容: 1.WIFI如何开启 2.扫描热点 3.连接热点 4. 断开热点 5.关闭WIFI 本节使用的是wpa_supplicant工具,它主要包含wpa_supplicant(命令行模式)与 ...
- 22.QT-QXmlStreamReader解析,QXmlStreamWriter写入
XML介绍 XML 用于存储数据,数据的形式类似于树结构(参考: http://www.runoob.com/xml/) 示例如下 <?xml version="1.0" e ...
- Eclipse中SVN插件的安装和配置(离线安装)
Eclipse利用svn的离线安装包进行配置svn,这种方式配置起来很简单,但是前提是必须下载可用的的svn离线包.因为有的从网上下载的svn离线包有问题. 第一步:下载svn离线包 我下载的是下面这 ...
- web站点和windows服务项目发布时如何排除指定文件
在发布asp.net站点和windows服务项目时,有的时候这样的需求:msbuild编译之后发布到服务器指定目录时要排除指定文件,比如通过jenkins构建时,不希望覆盖原来的Web.config和 ...
- markdown 语法指南
说明:左边是markdown的语法 右边是预览.(我这里用了黑色的背景,一般白色较多) 1. 标题 2.列表 3.引用 (1)一层引用 (2)多层引用 4.图片(如果是本地:按照语法写图片路径:如果是 ...