当前位置:网站首页>leetcode刷题:栈与队列02(用队列实现栈)
leetcode刷题:栈与队列02(用队列实现栈)
2022-07-01 19:16:00 【涛涛英语学不进去】
225. 用队列实现栈
使用队列实现栈的下列操作:
- push(x) – 元素 x 入栈
- pop() – 移除栈顶元素
- top() – 获取栈顶元素
- empty() – 返回栈是否为空
注意:
- 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。
- 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
- 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
对于这题,首先要知道几个方法
- pop 栈尾出元素
- push 栈尾入元素
- peek 查看栈首元素
- offer 队尾入元素
- poll 队头出元素
- peek 查看队列元素
我用的是ArrayDeque模拟的。其实查看API这个类有很多重复的方法。
boolean add(E e)
将指定元素插入此双端队列的末尾。
void addFirst(E e)
将指定元素插入此双端队列的开头。
void addLast(E e)
将指定元素插入此双端队列的末尾。
void clear()
从此双端队列中移除所有元素。
ArrayDeque clone()
返回此双端队列的副本。
boolean contains(Object o)
如果此双端队列包含指定元素,则返回 true。
Iterator descendingIterator()
返回以逆向顺序在此双端队列的元素上进行迭代的迭代器。
E element()
获取,但不移除此双端队列所表示的队列的头。
E getFirst()
获取,但不移除此双端队列的第一个元素。
E getLast()
获取,但不移除此双端队列的最后一个元素。
boolean isEmpty()
如果此双端队列未包含任何元素,则返回 true。
Iterator iterator()
返回在此双端队列的元素上进行迭代的迭代器。
boolean offer(E e)
将指定元素插入此双端队列的末尾。
boolean offerFirst(E e)
将指定元素插入此双端队列的开头。
boolean offerLast(E e)
将指定元素插入此双端队列的末尾。
E peek()
获取,但不移除此双端队列所表示的队列的头;如果此双端队列为空,则返回 null。
E peekFirst()
获取,但不移除此双端队列的第一个元素;如果此双端队列为空,则返回 null。
E peekLast()
获取,但不移除此双端队列的最后一个元素;如果此双端队列为空,则返回 null。
E poll()
获取并移除此双端队列所表示的队列的头(换句话说,此双端队列的第一个元素);如果此双端队列为空,则返回 null。
E pollFirst()
获取并移除此双端队列的第一个元素;如果此双端队列为空,则返回 null。
E pollLast()
获取并移除此双端队列的最后一个元素;如果此双端队列为空,则返回 null。
E pop()
从此双端队列所表示的堆栈中弹出一个元素。
void push(E e)
将元素推入此双端队列所表示的堆栈。
E remove()
获取并移除此双端队列所表示的队列的头。
boolean remove(Object o)
从此双端队列中移除指定元素的单个实例。
E removeFirst()
获取并移除此双端队列第一个元素。
boolean removeFirstOccurrence(Object o)
移除此双端队列中第一次出现的指定元素(当从头部到尾部遍历双端队列时)。
E removeLast()
获取并移除此双端队列的最后一个元素。
boolean removeLastOccurrence(Object o)
移除此双端队列中最后一次出现的指定元素(当从头部到尾部遍历双端队列时)。
int size()
返回此双端队列中的元素数。
Object[] toArray()
返回一个以恰当顺序包含此双端队列所有元素的数组(从第一个元素到最后一个元素)。
T[]
toArray(T[] a)
返回一个以恰当顺序包含此双端队列所有元素的数组(从第一个元素到最后一个元素);返回数组的运行时类型是指定数组的运行时类型。
package com.programmercarl.stacks_queues;
import java.util.ArrayDeque;
/** * @ClassName MyStack * @Descriotion TODO * @Author nitaotao * @Date 2022/6/29 12:10 * @Version 1.0 * https://leetcode.cn/problems/implement-stack-using-queues/ * 225. 用队列实现栈 * 思路: * 栈 先进后出 只能A端进,A端出 * 队列 先进先出 只能A端进,B端出 * pop 栈尾出元素 * push 栈尾入元素 * peek 查看栈首元素 * * offer 队尾入元素 * poll 队头出元素 * peek 查看队列元素 * * **/
public class MyStack {
//源队列
private ArrayDeque queueOrigin;
//复制队列
private ArrayDeque queueCopy;
public MyStack() {
queueOrigin = new ArrayDeque();
queueCopy = new ArrayDeque();
}
/** * 推入一个元素 * @param x */
public void push(int x) {
//源队列尾部推入数据
queueOrigin.offer(x);
}
/** * 推出一个元素 * @return */
public int pop() {
/** * 源队列把除了 最后一个元素,其余所有元素推入复制队列 */
int lenOrigin = queueOrigin.size();
while (lenOrigin > 1) {
queueCopy.offer(queueOrigin.poll());
lenOrigin--;
}
//获取要推出的值
int result = (int) queueOrigin.poll();
/** * 复制队列再把所有元素推入原队列 */
int lenCopy = queueCopy.size();
while (lenCopy > 0) {
queueOrigin.offer(queueCopy.poll());
lenCopy--;
}
return result;
}
/** * 查看顶部元素 * @return */
public int top() {
/** * 源队列把除了 最后一个元素,其余所有元素推入复制队列 */
int lenOrigin = queueOrigin.size();
while (lenOrigin > 1) {
queueCopy.offer(queueOrigin.poll());
lenOrigin--;
}
//获取要推出的值
int result = (int) queueOrigin.peek();
queueCopy.offer(queueOrigin.poll());
/** * 复制队列再把所有元素推入原队列 */
int lenCopy = queueCopy.size();
while (lenCopy > 0) {
queueOrigin.offer(queueCopy.poll());
lenCopy--;
}
return result;
}
/** * 判空 * @return */
public boolean empty() {
return queueOrigin.size() == 0;
}
}

思路其实不太好想到,毕竟每次用元素都要重新把所有元素放到另一个地方 ,再取这个元素。这种低效率且属于脱裤子放屁的事现实中没必要这么用。代码不难,都有注释,自己看看就好了。
边栏推荐
- C # joint Halcon application - Dahua camera acquisition class
- 关联线探究,如何连接流程图的两个节点
- 新版Free手机、PC、平板、笔记本四端网站缩略展示图在线一键生成网站源码
- What if the win11 shortcut key switching input method doesn't respond? Shortcut key switching input method does not respond
- Using qeventloop to realize synchronous waiting for the return of slot function
- PHP获取微信小程序和小程序商店外链地址
- What did you learn about cheating after you went to college?
- Iframe parent-child page communication
- 喜马拉雅自研网关架构演进过程
- 基于图的 Affinity Propagation 聚类计算公式详解和代码示例
猜你喜欢

2022年低压电工考试试题及答案

Items in richview documents

目标检测——Yolo系列

走进如心小镇,数智化变革连接“未来社区”

Customize the insertion of page labels and realize the initial search of similar address books

8K HDR!| Hevc hard solution for chromium - principle / Measurement Guide

Arduino stepper library drive 28byj-48 stepper motor test program

Use Zadig to build a continuous delivery platform from 0 to 1

Win11 how to hide the taskbar? Win11 method to hide the taskbar

Vulnerability recurrence - Net ueeditor upload
随机推荐
How to create a pyramid with openmesh
目標檢測——Yolo系列
[Blue Bridge Cup web] analysis of the real topic of the 13th Blue Bridge Cup web university group match in 2022
On the usage of a magic function
Error in installing sharp
After adding cocoapods successfully, the header file cannot be imported or an error is reported in not found file
【蓝桥杯Web】2022年第十三届蓝桥杯Web大学组国赛真题解析
tensorflow 张量做卷积,输入量与卷积核维度的理解
docker ubuntu容器中安装mysql遇到的问题
Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
实战项目笔记(一)——虚拟机的创建
Develop those things: easycvr platform adds playback address authentication function
Learn white box test case design from simple to deep
强大、好用、适合程序员/软件开发者的专业编辑器/笔记软件综合评测和全面推荐
漏洞复现-.Net-ueditor上传
PLC模拟量输入 模拟量转换FB S_ITR(三菱FX3U)
Détection des cibles - série Yolo
编译原理复习笔记
写博客文档