刷题 图论

news2024/10/8 14:46:46

面试经典 150 题 - 图

200. 岛屿数量

在这里插入图片描述

dfs 标记 visited

class Solution {
public:
    // dfs 染色
    const int direction[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
    void dfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y) {
        int n = grid.size(), m = grid[0].size();
        visited[x][y] = true;
        for (int i = 0; i < 4; ++i) {
            int new_x = x + direction[i][0], new_y = y + direction[i][1];
            if (new_x < 0 || new_x >= n || new_y < 0 || new_y >= m || grid[new_x][new_y] == '0' || visited[new_x][new_y] == true) {
                continue;
            }
            visited[new_x][new_y] = true;
            dfs(grid, visited, new_x, new_y);
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<bool>> visited(n, vector<bool>(m, false));
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == '1' && visited[i][j] == false) {
                    ++ans;
                    // dfs 染色 将相邻区域中的 1 全部标记为 visited
                    dfs(grid, visited, i, j);
                }
            }
        }
        return ans;
    }
};

bfs 标记 visited

class Solution {
public:
    // bfs 染色
    const int direction[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
    void bfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y) {
        int n = grid.size(), m = grid[0].size();
        queue<pair<int,int>> que;
        que.push(make_pair(x, y));
        visited[x][y] = true;
        while (!que.empty()) {
            int cur_x = que.front().first, cur_y = que.front().second;
            que.pop();
            for (int i = 0; i < 4; ++i) {
                int new_x = cur_x + direction[i][0], new_y = cur_y + direction[i][1];
                if (new_x < 0 || new_x >= n || new_y < 0 || new_y >= m || grid[new_x][new_y] == '0' || visited[new_x][new_y] == true) {
                    continue;
                }
                que.push(make_pair(new_x, new_y));
                visited[new_x][new_y] = true;
            }
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<bool>> visited(n, vector<bool>(m, false));
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == '1' && visited[i][j] == false) {
                    ++ans;
                    // bfs 染色 将相邻区域中的 1 全部标记为 visited
                    bfs(grid, visited, i, j);
                }
            }
        }
        return ans;
    }
};

⭐️⭐️130. 被围绕的区域

从四周出发进行 bfs
在这里插入图片描述

class Solution {
public:
    // 想法一:
    // 不被围绕的区域,也即在 bfs 或者 dfs 过程中邻域中出现越界现象
    // 简单的想法: 在bfs 和 dfs 过程中记录所有坐标 以及 一个标志位
    // 如果没有出现越界,就将坐标对应的所有值赋值为 'X'

    // 想法二:
    // 更简单的想法,直接从边界上的 'O' 处出发即可,将连通域全部标记为visited
    // 最后遍历 visited 数组即可

    const int direction[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
    void dfs(vector<vector<char>>& grid, vector<vector<bool>>& visited, int x, int y) {
        int n = grid.size(), m = grid[0].size();
        visited[x][y] = true;
        for (int i = 0; i < 4; ++i) {
            int new_x = x + direction[i][0], new_y = y + direction[i][1];
            if (new_x < 0 || new_x >= n || new_y < 0 || new_y >= m || grid[new_x][new_y] == 'X' || visited[new_x][new_y] == true) {
                continue;
            }
            visited[new_x][new_y] = true;
            dfs(grid, visited, new_x, new_y);
        }
    }
    void solve(vector<vector<char>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<bool>> visited(n, vector<bool>(m, false));
        for (int i = 0; i < n; ++i) {
            if (grid[i][0] == 'O' && visited[i][0] == false) {
                dfs(grid, visited, i, 0);
            }
            if (grid[i][m-1] == 'O' && visited[i][m-1] == false) {
                dfs(grid, visited, i, m-1);
            }
        }
        for (int j = 1; j < m - 1; ++j) {
            if (grid[0][j] == 'O' && visited[0][j] == false) {
                dfs(grid, visited, 0, j);
            }
            if (grid[n-1][j] == 'O' && visited[n-1][j] == false) {
                dfs(grid, visited, n-1, j);
            }
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] == 'O' && visited[i][j] == false) {
                    grid[i][j] = 'X';
                }
            }
        }
    }
};

⭐️⭐️⭐️133. 克隆图

在这里插入图片描述

bfs 用哈希表记录节点是否访问过以及与深拷贝之间的对应关系

// 题目要求返回 图 的深拷贝
// 所谓深拷贝,也即需要新建一个节点,而不是使用原始节点,只是和原始节点的值相同
class Solution {
public:
    Node* cloneGraph(Node* node) {
        if (node == nullptr) return nullptr;
        Node* new_node = new Node(node->val);
        queue<Node*> que;
        unordered_map<Node*, Node*> map;
        que.push(node);
        map[node] = new_node;
        while (!que.empty()) {
            Node* cur = que.front();
            que.pop();
            for (auto next : cur->neighbors) {
                if (map.find(next) == map.end()) {
                    // 出现新节点 --> 需要创建
                    Node* new_next = new Node(next->val);
                    que.push(next);
                    map[next] = new_next;
                }
                map[cur]->neighbors.push_back(map[next]);
            }
        }
        return new_node;
    }
};

⭐️⭐️399. 除法求值

在这里插入图片描述

dfs 寻找可达路径

邻接矩阵和 01 矩阵的区别,一个使用 unordered_set 标记是否走过,一个使用 visited 矩阵标记是否走过

class Solution {
public:
    // 本题也即构建一个无向图,查找 节点之间 存在的路径
    // 查找路径我们可以使用 dfs
    struct Edge {
        string node;
        double val;
    };
    bool dfs(string& src, string& dst, unordered_map<string, vector<Edge>>& graph, unordered_set<string>& visited, vector<double> &path) {
        visited.insert(src);
        if (src == dst) {
            return true;
        }
        for (auto edge : graph[src]) {
            if (visited.find(edge.node) == visited.end()) {
                path.push_back(edge.val);
                if (dfs(edge.node, dst, graph, visited, path)) {
                    return true;
                }
                path.pop_back();
            }
        }
        return false;
    }
    vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
        // 构建无向图
        unordered_map<string, vector<Edge>> graph;
        for (int i = 0; i < equations.size(); ++i) {
            auto& equation = equations[i];
            graph[equation[0]].push_back({equation[1], values[i]});
            graph[equation[1]].push_back({equation[0], 1.0 / values[i]});
        }
        // 遍历查询
        vector<double> result(queries.size(), 0);
        for (int i = 0; i < queries.size(); ++i) {
            string& src = queries[i][0], &dst = queries[i][1];
            if (graph.find(src) == graph.end() || graph.find(dst) == graph.end()) {
                result[i] = -1.0;
                continue;
            }
            // dfs 寻找路径
            vector<double> path;
            unordered_set<string> visited;
            if (dfs(src, dst, graph, visited, path)) {
                double ans = 1.0;
                for (auto p : path) {
                    ans *= p;
                }
                result[i] = ans;
            } else {
                result[i] = -1;
            }

        }
        return result;
    }
};

207. 课程表

在这里插入图片描述

拓扑排序

class Solution {
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        // 想法:逐个剔除入度为 0 的节点
        // + 如何获得每个节点的入度?
        // + 如何剔除节点?
        // prerequisites[i] = [ai, bi] 说明存在有向边 bi -> ai
        // 解决方案:可以用一个二维矩阵作为邻接矩阵,再用一个vector存储节点的入度
        vector<vector<bool>> adj(numCourses, vector<bool>(numCourses, false));
        vector<int> in_degree(numCourses, 0);
        queue<int> zero_in_nodes;
        for (auto& pre : prerequisites) {
            adj[pre[1]][pre[0]] = true;
            in_degree[pre[0]]++;
        }
        for (int i = 0; i < numCourses; ++i) {
            if (in_degree[i] == 0) {
                zero_in_nodes.push(i);
            }
        }
        while (!zero_in_nodes.empty()) {
            // 找到入度为0的节点
            int x = zero_in_nodes.front();
            zero_in_nodes.pop();
            // 删除节点
            for (int i = 0; i < numCourses; ++i) {
                if (adj[x][i]) {
                    adj[x][i] = false;
                    adj[i][x] = false;
                    if (--in_degree[i] == 0) {
                        zero_in_nodes.push(i);
                    }
                }
            }
        }
        for (int i = 0; i < numCourses; ++i) {
            if (in_degree[i] > 0) {
                return false;
            }
        }
        return true;
    }
};

⭐️⭐️210. 课程表 II

在这里插入图片描述

class Solution {
public:
    // 和课程表 1 是一样的
    // 逐渐剔除入度为0的节点
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<int> result;

        // 统计入度
        vector<int> in_degree(numCourses, 0);
        for (auto& prerequisite : prerequisites) {
            in_degree[prerequisite[1]]++;
        }

        // 统计邻接表: 删除节点的时候需要根据邻接表更新指向节点的入度
        vector<vector<int>> adj(numCourses);
        for (auto& prerequisite : prerequisites) {
            adj[prerequisite[0]].push_back(prerequisite[1]);
        }

        // 初始化队列,存储入度为0的节点
        queue<int> que;
        for (int i = 0; i < numCourses; ++i) {
            if (in_degree[i] == 0) {
                que.push(i);
            }
        }
        // 遍历队列
        while (!que.empty()) {
            // 将入度为0节点弹出队列
            int node_from = que.front();
            que.pop();
            result.push_back(node_from);
            // 根据邻接表更新入度,入度为0加入队列
            for (auto& node_to : adj[node_from]) {
                if (--in_degree[node_to] == 0) {
                    que.push(node_to);
                }
            }
        }
        if (result.size() == numCourses) {
            std::reverse(result.begin(), result.end());
        } else {
            return std::vector<int>{};
        }
        return result;
    }
};

面试经典 150 题 - 图的广度优先搜索 - 最短路径

⭐️⭐️909. 蛇梯棋

在这里插入图片描述

bfs 最短路径长度:在队列中记录 step / 使用parent 数组

  • bfs 找最短路径 需要使用 parent数组来进行 回溯
  • 题目中的梯子和蛇 只是起到 传送作用而已,也即掷完骰子后如果到达一个梯子的起点就需要手动执行传送 next = adj[next]
class Solution {
public:
    int snakesAndLadders(vector<vector<int>>& board) {
        int n = board.size();
        vector<int> adj(n * n + 1, -1);
        // 构建 adj 数组,映射每个位置对应的蛇或梯子
        int label = 1;
        for (int i = n - 1; i >= 0; --i) {
            if ((n - 1 - i) % 2 == 0) {
                for (int j = 0; j < n; ++j) {
                    if (board[i][j] != -1) {
                        adj[label] = board[i][j];
                    }
                    ++label;
                }
            } else {
                for (int j = n - 1; j >= 0; --j) {
                    if (board[i][j] != -1) {
                        adj[label] = board[i][j];
                    }
                    ++label;
                }
            }
        }
        // BFS 进行最短路径搜索
        vector<bool> visited(n * n + 1, false);
        vector<int> parent(n * n + 1, -1);  // 记录每个节点的父节点,用于路径回溯
        queue<int> que;
        que.push(1);
        visited[1] = true;
        while (!que.empty()) {
            auto cur = que.front();
            que.pop();
            if (cur == n * n) { // 回溯路径
                int count = 0;
                int node = cur;
                while (node != 1) {
                    ++count;
                    node = parent[node];
                }
                return count;
            }
            // 掷骰子
            for (int i = 1; i <= 6; ++i) {
                int next = cur + i;
                if (next > n * n) break;
                // 如果有梯子或蛇, 则从 next 传送到 adj[next]
                if (adj[next] != -1) {
                    next = adj[next];
                }
                if (!visited[next]) {
                    visited[next] = true;
                    parent[next] = cur;  // 记录父节点
                    que.push(next);
                }
            }
        }
        return -1;  // 无法到达终点
    }
};

⭐️⭐️433. 最小基因变化

在这里插入图片描述

bfs 最短路径长度:在队列中记录 step

class Solution {
public:
    bool check(string& s1, string& s2) {
        int count = 0;
        for (int i = 0; i < s1.size(); ++i) {
            count += (s1[i] != s2[i]);
            if (count > 1) {
                return false;
            }
        }
        return true;
    }
    // 起始序列不一定在bank中
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        // 先检查一下终止序列是否在bank中
        int n = bank.size();
        int src = -1, dst = -1;
        for (int i = 0; i < n; ++i) {
            if (bank[i] == endGene) {
                dst = i;
            }
            if (bank[i] == startGene) {
                src = i;
            }
        }
        if (dst == -1) return -1;
        if (src == -1) src = n;
        // 构建邻接表
        vector<vector<int>> adj(n + 1);
        // src 到 bank 是单向的
        if (src == n) {
            for (int i = 0; i < n; ++i) {
                if (check(startGene, bank[i])) {
                    adj[n].push_back(i);
                }
            }
        }
        for (int i = 0; i < n - 1; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (check(bank[i], bank[j])) {
                    adj[i].push_back(j);
                    adj[j].push_back(i);
                }
            }
        }
        // bfs 搜索路径长度
        vector<bool> visited(n + 1, false);
        queue<pair<int, int>> que;
        que.push({src, 0});
        visited[src] = true;
        while (!que.empty()) {
            auto [cur, step] = que.front();
            if (cur == dst) {
                return step;
            }
            que.pop();
            for (auto& next : adj[cur]) {
                if (visited[next] == false) {
                    que.push({next, step + 1});
                    visited[next] = true;
                }
            }
        }
        return -1;
    }
};

双向bfs:两边分别使用一个visited数组记录path长度,根据两个visited数组来判断是否,优先扩展较小的搜索方向

通过设置条件,当某一方向的队列长度显著小于另一方向时,可以优先展开该方向的搜索,避免不必要的广度扩展。

class Solution {
public:
    bool check(const string& s1, const string& s2) {
        int count = 0;
        for (int i = 0; i < s1.size(); ++i) {
            if (s1[i] != s2[i] && ++count > 1) {
                return false;
            }
        }
        return true;
    }

    int minMutation(string startGene, string endGene, vector<string>& bank) {
        int n = bank.size();
        int src = -1, dst = -1;

        // 提前记录起点和终点
        for (int i = 0; i < n; ++i) {
            if (bank[i] == endGene) dst = i;
            if (bank[i] == startGene) src = i;
        }

        if (dst == -1) return -1;
        if (src == -1) src = n;  // 起始序列不在 bank 中

        // 构建邻接表,减少不必要的 check 调用
        vector<vector<int>> adj(n + 1);
        if (src == n) {
            for (int i = 0; i < n; ++i) {
                if (check(startGene, bank[i])) {
                    adj[n].push_back(i);
                }
            }
        }

        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (check(bank[i], bank[j])) {
                    adj[i].push_back(j);
                    adj[j].push_back(i);
                }
            }
        }

        // 使用双向 BFS
        vector<int> visited_forward(n + 1, -1);
        vector<int> visited_back(n + 1, -1);
        queue<pair<int, int>> que_forward;
        queue<pair<int, int>> que_back;

        que_forward.push({src, 0});
        que_back.push({dst, 0});
        visited_forward[src] = 0;
        visited_back[dst] = 0;

        // 优化 BFS 方向的扩展
        while (!que_forward.empty() && !que_back.empty()) {
            // 优先扩展较小的搜索方向
            if (que_forward.size() <= que_back.size()) {
                auto [cur, steps] = que_forward.front();
                que_forward.pop();
                for (int next : adj[cur]) {
                    if (visited_forward[next] == -1) {
                        visited_forward[next] = steps + 1;
                        if (visited_back[next] != -1) {
                            return visited_forward[next] + visited_back[next];
                        }
                        que_forward.push({next, steps + 1});
                    }
                }
            } else {
                auto [cur, steps] = que_back.front();
                que_back.pop();
                for (int next : adj[cur]) {
                    if (visited_back[next] == -1) {
                        visited_back[next] = steps + 1;
                        if (visited_forward[next] != -1) {
                            return visited_forward[next] + visited_back[next];
                        }
                        que_back.push({next, steps + 1});
                    }
                }
            }
        }
        return -1;
    }
};

127. 单词接龙

在这里插入图片描述

和最小基因变化是一样的,只不过长度需要加1,不成立的话返回0而不是-1

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2196576.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

哪些人群适合参加六西格玛绿带培训?

六西格玛作为一种全球公认的质量管理方法论&#xff0c;凭借其强大的数据分析和流程改进能力&#xff0c;成为众多企业转型升级的重要工具。而六西格玛绿带培训&#xff0c;作为连接黄带和黑带之间的桥梁&#xff0c;更是吸引了来自不同行业和职位的众多人士。那么&#xff0c;…

理解C语言之深入理解指针(五)

目录 1. sizeof和strlen的对⽐ 1.1 sizeo 1.2 strlen 1.3 sizeof和strlen的对⽐ 2. 数组和指针笔试题解析 2.1 ⼀维数组 2.2 字符数组 2.3 ⼆维数组 3. 指针运算笔试题解析 3.1 题⽬1&#xff1a; 3.2 题⽬2 3.3 题⽬3 3.4 题⽬4 3.5 题⽬5 3.6 题⽬6 3.7 题⽬…

鸿蒙开发之ArkUI 界面篇 二十一 人气卡片综合案例

要实现如下图效果&#xff1a; 仔细分析效果&#xff0c;整体分为三个区域&#xff0c;分别是1、2、3&#xff0c;如图所示 我们整体分析&#xff0c;区域1是观察到的是图片&#xff0c;自然是Image组件&#xff0c;区域2有个背景&#xff0c;左边是Image&#xff0c;水平方向…

《Spring Microservices in Action, 2nd Edition》读后总结

总体来说有种时过境迁的感觉&#xff0c;有些章节的内容已经跟不上现在&#xff0c;特别对于云原生大行其道的当下&#xff0c; 越来越多东西下沉到基础设施层&#xff0c;然后应用层尽量轻量化成了一种新趋势&#xff1b;当然任何事物都具有多面性&#xff0c;云原生那套也要投…

21世纪现代国学四大泰斗颜廷利教授:一位多面兼具深度的思想家

颜廷利&#xff0c;出生于1971年10月15日的这位杰出人物&#xff0c;来自中国山东省济南市的一个平凡家庭。他在北京大学接受了高等教育&#xff0c;专攻哲学和教育学&#xff0c;深入探索了东西方哲学理论。他的研究领域涵盖了哲学、文化、经济等多个领域&#xff0c;并在易经…

【element-tiptap】报错Duplicate use of selection JSON ID cell at Selection.jsonID

我是下载了element-tiptap 给出的示例项目&#xff0c;在本地安装依赖、运行报错了&#xff0c; 报错截图&#xff1a; 在项目目录下找 node_modules/tiptap-extensions/node-modules&#xff0c;把最后的 node-modules 目录名字修改一下&#xff0c;例如修改为 node-modules–…

亨廷顿舞蹈症患者必知的营养补充指南

在生活的舞台上&#xff0c;每个人都是自己故事的主角&#xff0c;即使面对如亨廷顿舞蹈症&#xff08;HD&#xff09;这样的挑战&#xff0c;我们依然可以通过科学的饮食管理&#xff0c;为健康之路增添更多希望与色彩。今天&#xff0c;就让我们一起探索亨廷顿舞蹈症患者应该…

【汇编语言】寄存器(CPU工作原理)(四)—— “段地址x16 + 偏移地址 = 物理地址”的本质含义以及段的概念和小结

文章目录 前言1. "段地址x16 偏移地址 物理地址"的本质含义2. 段的概念3. 内存单元地址小结结语 前言 &#x1f4cc; 汇编语言是很多相关课程&#xff08;如数据结构、操作系统、微机原理&#xff09;的重要基础。但仅仅从课程的角度出发就太片面了&#xff0c;其实…

单片机教案 1.1 ATmega2560单片机概述

第一章 迈进单片机的大门 Arduino是一款便捷灵活、方便上手的开源电子原型平台&#xff0c;为迈进单片机的大门提供了良好的入门途径。以下是对Arduino的详细介绍&#xff1a; 一、Arduino简介 Arduino是一个能够用来感应和控制现实物理世界的一套工具&#xff0c;它由一个基…

C++ 基于SDL库的 Visual Studio 2022 环境配置

系统&#xff1a;w10、编辑器&#xff1a;Visual Studio 2022、 下载地址 必要库&#xff1a; SDL https://github.com/libsdl-org/SDL 字体 https://github.com/libsdl-org/SDL_ttf 图片 https://github.com/libsdl-org/SDL_image 音频 https://github.com/libsdl-org/SDL_m…

连续点击三次用户

有用户点击日志记录表 t2_click_log&#xff0c;包含user_id(用户ID),click_time(点击时间)&#xff0c;请查询出连续点击三次的用户数&#xff0c; 连续点击三次&#xff1a;指点击记录中同一用户连续点击&#xff0c;中间无其他用户点击&#xff1b; CREATE TABLE t2_click…

两个div中间有缝隙

两个div中间有缝隙效果图&#xff1a; 这种是display:inline-block造成的 在父元素中加入font-size:0px;&#xff0c;再在相应的子div中加入font-size:12px;就可以了 调整后效果图&#xff1a;

Pandas和Seaborn数据可视化

Pandas数据可视化 学习目标 本章内容不需要理解和记忆&#xff0c;重在【查表】&#xff01; 知道数据可视化的重要性和必要性知道如何使用Matplotlib的常用图表API能够找到Seaborn的绘图API 1 Pandas数据可视化 一图胜千言&#xff0c;人是一个视觉敏感的动物&#xff0c;大…

数据库-分库分表

什么是分库分表 分库分表是一种数据库优化策略。 目的&#xff1a;为了解决由于单一的库表数据量过大而导致数据库性能降低的问题 分库&#xff1a;将原来独立的数据库拆分成若干数据库组成 分表&#xff1a;将原来的大表(存储近千万数据的表)拆分成若干个小表 什么时候考虑分…

Web 性能优化|了解 HTTP 协议后才能理解的预加载

作者&#xff1a;谦行 一、前言 在性能优化过程中&#xff0c;开发者通常会集中精力在以下几个方面&#xff1a;服务器响应时间&#xff08;RT&#xff09;优化、服务端渲染&#xff08;SSR&#xff09;与客户端渲染优化、以及静态资源体积的减少。然而&#xff0c;对于许多用…

C(十五)函数综合(一)--- 开公司吗?

在这篇文章中&#xff0c;杰哥将带大家 “开公司”。 主干内容部分&#xff08;你将收获&#xff09;&#xff1a;&#x1f449; 为什么要有函数&#xff1f;函数有哪些&#xff1f;怎么自定义函数以及获得函数的使用权&#xff1f;怎么对函数进行传参&#xff1f;函数中变量的…

[嵌入式Linux]—STM32MP1启动流程

STM32MP1启动流程 1.启动模式 STM32MP1等SOC支持从多种设备中启动&#xff0c;如EMMC、SD、NAND、NOR、USB、UART等。其中USB、UART是作为烧录进行启动的。 STM32MP1内部ROM中存储有一段出厂代码来进行判断从哪种设备中启动&#xff0c;上电后这段代码会被执行&#xff0c;这…

使用java函数逆序一个单链表

代码功能 定义了一个ListNode类&#xff0c;用于表示单链表的节点&#xff0c;每个节点包含一个整数值和一个指向下一个节点的引用。 在ReverseLinkedList类的main方法中&#xff0c;创建了一个包含从1到10的整数的单链表。 定义了一个printList方法&#xff0c;用于打印链表的…

基于JavaWeb开发的java springmvc+mybatis酒水商城管理系统设计和实现

基于JavaWeb开发的java springmvcmybatis酒水商城管理系统设计和实现 &#x1f345; 作者主页 网顺技术团队 &#x1f345; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; &#x1f345; 文末获取源码联系方式 &#x1f4dd; &#x1f345; 查看下方微信号获取联系方式 承…

【JAVA基础】集合类之HashSet的原理及应用

近期几期内容都是围绕该体系进行知识讲解&#xff0c;以便于同学们学习Java集合篇知识能够系统化而不零散。 本文将介绍HashSet的基本概念&#xff0c;功能特点&#xff0c;使用方法&#xff0c;以及优缺点分析和应用场景案例。 一、概念 HashSet是 Java 集合框架中的一个重…