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

相关文章

多源 BFS

例题一 解法(bfs)(多个源头的最短路问题) 算法思路: 对于求的最终结果,我们有两种⽅式: • 第⼀种⽅式:从每⼀个 1 开始,然后通过层序遍历找到离它最近的 0 。 这⼀种⽅式,我们会以所有的 1 起点,来⼀次层序遍历,势必会遍历到很多重复的点。并且如果 矩阵中只有⼀个 0 的话,每⼀次层序遍历都要遍历很多层,时间复

[ip核][vivado]FIFO 学习

<xlinx FPGA应用进阶 通用IP核详解和设计开发>读书摘录: 1.        2.3.仿真模型 特点总结:1)复位后会有busy状态,需要等待wr_rst_busy信号低电平后才能正常写入                  2)prog_full信号的高电平长度可调                  3)仿真中的读状态很奇怪,并没有正常读取,都是XXX的状态。 所用的te

LeetCode--231 2的幂

题目 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 示例 示例 1:输入: 1输出: true解释: 20 = 1示例 2:输入: 16输出: true解释: 24 = 16示例 3:输入: 218输出: false class Solution {public:bool isPowerOfTwo(int n) {if (n <= 0) return fals

LeetCode--234 回文链表

题目 请判断一个链表是否为回文链表。 示例 示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val

LeetCode--220 存在重复元素 III

题目 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 示例 1:输入: nums = [1,2,3,1], k = 3, t = 0输出: true示例 2:输入: nums = [1,0,1,1], k = 1, t = 2输出: true示例

LeetCode--217 存在重复元素

题目 给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 示例 示例 1:输入: [1,2,3,1]输出: true示例 2:输入: [1,2,3,4]输出: false示例 3:输入: [1,1,1,3,3,4,3,2,4,2]输出: true class Solution {p

LeetCode--214 最短回文串

题目 给定一个字符串 s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。 示例 示例 1:输入: "aacecaaa"输出: "aaacecaaa"示例 2:输入: "abcd"输出: "dcbabcd" 思路: 我们需要添加多少个字符与给定字符串的前缀子串回文的长度有关. 也就是说去掉其前缀的回文子串,我们只需要补充剩下的子串的逆序

LeetCode--206 反转链表

题目 反转一个单链表。 示例 示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL class Solution {public:ListNode* reverseList(ListNode* head) {if (head == nullptr || head->next == nullptr){return head;}ListNo

LeetCode--204 计数质数

题目 统计所有小于非负整数 n 的质数的数量。 示例 示例:输入: 10输出: 4解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 class Solution {public:int countPrimes(int n) {if (n <= 2) return 0;int cnt = 0;vector<bool> isPrime(n, true);

LeetCode--198 打家劫舍

题目 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。 示例 示例 1:输入: [1,2,3,1]输出: 4解释: 偷窃 1 号房屋 (金额 =