Leetcode刷题之——队列Queue|先入先出FIFO|广度优先搜索BFS|栈Stack|后入先出LIFO|深度优先搜索DFS

本文主要是介绍Leetcode刷题之——队列Queue|先入先出FIFO|广度优先搜索BFS|栈Stack|后入先出LIFO|深度优先搜索DFS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Leetcode刷题之——队列Queue|先入先出FIFO|广度优先搜索BFS|栈Stack|后入先出LIFO|深度优先搜索DFS

  • 1. 队列(Queue)——FIFO,先入先出的数据结构
    • 1.1 循环队列
    • 1.2 内置队列的常用方法(C++)
    • 1.3 广度优先搜索(BFS)
  • 2.栈(Stack)——LIFO, 后入先出的数据结构
    • 2.1 栈的用法(C++)
    • 2.2 深度优先搜索(DFS)

1. 队列(Queue)——FIFO,先入先出的数据结构

队列是一种典型的FIFO数据结构。

FIFO的数据结构中,将首先处理添加到队列中的第一个元素。

入队(Enqueue):队列中,插入(insert)称作入队, 新插入的元素将被添加到队列的末尾。

出队(Dequeue):出队时, 与入队相反,首先被操作的,是第一个元素。

在这里插入图片描述

1.1 循环队列

普通队列就不讲了,一旦一个队列满了,即使在队列前面仍有空间也不能插入下一个元素,这在实际上并不常用。
循环队列就是主要设计出来解决上述问题的。

class MyCircularQueue {
private:vector<int> data;int head;int tail;int size;
public:/** Initialize your data structure here. Set the size of the queue to be k. */MyCircularQueue(int k) {data.resize(k);head = -1;tail = -1;size = k;}/** Insert an element into the circular queue. Return true if the operation is successful. */bool enQueue(int value) {if (isFull()) {return false;}if (isEmpty()) {head = 0;}tail = (tail + 1) % size;data[tail] = value;return true;}/** Delete an element from the circular queue. Return true if the operation is successful. */bool deQueue() {if (isEmpty()) {return false;}if (head == tail) {head = -1;tail = -1;return true;}head = (head + 1) % size;return true;}/** Get the front item from the queue. */int Front() {if (isEmpty()) {return -1;}return data[head];}/** Get the last item from the queue. */int Rear() {if (isEmpty()) {return -1;}return data[tail];}/** Checks whether the circular queue is empty or not. */bool isEmpty() {return head == -1;}/** Checks whether the circular queue is full or not. */bool isFull() {return ((tail + 1) % size) == head;}
};/*** Your MyCircularQueue object will be instantiated and called as such:* MyCircularQueue obj = new MyCircularQueue(k);* bool param_1 = obj.enQueue(value);* bool param_2 = obj.deQueue();* int param_3 = obj.Front();* int param_4 = obj.Rear();* bool param_5 = obj.isEmpty();* bool param_6 = obj.isFull();*/

1.2 内置队列的常用方法(C++)

当你想要按顺序处理元素时,使用队列可能是一个很好的选择。不过大多数流行语言都提供内置的队列库,因此无需自己重新发明轮子。
empty(): 判空 (和stack一样)
pop(): 出 (和stack一样)
push(): 进 (和stack, vector一样)
front(): 获取第一个
back():获取最后一个

1.3 广度优先搜索(BFS)

广度优先搜索(BFS)的一个常见应用是找出从根结点到目标结点的最短路径.
第一轮处理根结点;
第二轮处理根结点旁边的结点;
第三轮处理距根结点两步的结点;
如果在第 k 轮中将结点 X 添加到队列中,则根结点与 X 之间的最短路径的长度恰好是 k
在这里插入图片描述BFS的模板代码(JAVA)
每一轮中,逐个处理已经在队列中的结点,并将所有邻居添加到队列中。新添加的节点不会立即遍历,而是在下一轮中处理。

/*** Return the length of the shortest path between root and target node.*/
int BFS(Node root, Node target) {Queue<Node> queue;  // store all nodes which are waiting to be processedint step = 0;       // number of steps neeeded from root to current node// initializeadd root to queue;// BFSwhile (queue is not empty) {step = step + 1;// iterate the nodes which are already in the queueint size = queue.size();for (int i = 0; i < size; ++i) {Node cur = the first node in queue;return step if cur is target;for (Node next : the neighbors of cur) {add next to queue;}remove the first node from queue;}}return -1;          // there is no path from root to target
}

2.栈(Stack)——LIFO, 后入先出的数据结构

栈是一种典型的LIFO数据结构。

LIFO的数据结构中,将首先处理添加到队列中的第一个元素。

入栈(Push):栈中,插入操作被称为入栈, 新插入的元素将被添加到堆栈的末尾。

出栈(Pop):出栈时, 与入栈相同,首先被操作的,是最后一个元素。

在这里插入图片描述

2.1 栈的用法(C++)

push(): 入栈
pop(): 退栈
empty(): 判空
top(): 获取第一个

2.2 深度优先搜索(DFS)

深度优先搜索(DFS)也可用于查找从根结点到目标结点的路径。与 BFS 不同,更早访问的结点可能不是更靠近根结点的结点。因此,在 DFS 中找到的第一条路径可能不是最短路径
在这里插入图片描述
DFS的模板代码(JAVA)
显式栈实现 DFS:

/** Return true if there is a path from cur to target.*/
boolean DFS(int root, int target) {Set<Node> visited;Stack<Node> s;add root to s;while (s is not empty) {Node cur = the top element in s;return true if cur is target;for (Node next : the neighbors of cur) {if (next is not in visited) {add next to s;add next to visited;}}remove cur from s;}return false;
}

这篇关于Leetcode刷题之——队列Queue|先入先出FIFO|广度优先搜索BFS|栈Stack|后入先出LIFO|深度优先搜索DFS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/936050

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

深度剖析SpringBoot日志性能提升的原因与解决

《深度剖析SpringBoot日志性能提升的原因与解决》日志记录本该是辅助工具,却为何成了性能瓶颈,SpringBoot如何用代码彻底破解日志导致的高延迟问题,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言第一章:日志性能陷阱的底层原理1.1 日志级别的“双刃剑”效应1.2 同步日志的“吞吐量杀手”

spring AMQP代码生成rabbitmq的exchange and queue教程

《springAMQP代码生成rabbitmq的exchangeandqueue教程》使用SpringAMQP代码直接创建RabbitMQexchange和queue,并确保绑定关系自动成立,简... 目录spring AMQP代码生成rabbitmq的exchange and 编程queue执行结果总结s

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security