Leetcode热题100-200 岛屿数量

Leetcode热题100-200 岛屿数量

  • [1. 题目描述](#1. 题目描述)
  • [2. 代码实现](#2. 代码实现)
    • [1. dfs算法](#1. dfs算法)
    • [2. bfs算法](#2. bfs算法)

1. 题目描述

200 岛屿数量

2. 代码实现

1. dfs算法

cpp 复制代码
class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size(), n = grid[0].size();
        int res = 0;

        // 注意lambda函数本身是不支持递归的,需要捕获自身来实现递归调用
        // 使用 lambda 表达式实现 DFS
        auto dfs = [&](auto&& dfs, int x, int y) -> void {
            // 超出边界检查
            if (x < 0 || x >= m || y < 0 || y >= n)
                return;

            // 不为陆地或者已访问过
            if (grid[x][y] != '1')
                return;

            // 设置为已访问
            grid[x][y] = '0';

            // 向四个方向递归
            dfs(dfs, x - 1, y); // 上
            dfs(dfs, x + 1, y); // 下
            dfs(dfs, x, y - 1); // 左
            dfs(dfs, x, y + 1); // 右
        };
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    dfs(dfs, i, j);
                    res += 1;
                }
            }
        }
        return res;
    }
};

2. bfs算法

cpp 复制代码
class Solution {
public:
    // 定义方向向量:上下左右
    vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int numIslands(vector<vector<char>>& grid) {
        int m = grid.size(), n = grid[0].size();
        int res = 0;

        // 使用bfs遍历的方式来实现
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == '1') {
                    queue<pair<int, int>> que;
                    que.push({i, j});
                    // 标记为已访问
                    grid[i][j] = '0';

                    while (!que.empty()) {
                        auto [x, y] = que.front();
                        que.pop();

                        // 遍历四个方向
                        for (const auto& [dx, dy] : dirs) {
                            int new_x = x + dx;
                            int new_y = y + dy;

                            // 判断是否越界以及是否为陆地
                            if (new_x >= 0 && new_x < m && new_y >= 0 &&
                                new_y < n && grid[new_x][new_y] == '1') {
                                que.push({new_x, new_y});
                                grid[new_x][new_y] = '0';
                            }
                        }
                    }
                    res++;
                }
            }
        }
        return res;
    }
};
相关推荐
weixin_446122469 分钟前
LinkedList剖析
算法
GiraKoo11 分钟前
【GiraKoo】C++14的新特性
c++
悠悠小茉莉22 分钟前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
坏柠36 分钟前
C++ Qt 基础教程:信号与槽机制详解及 QPushButton 实战
c++·qt
泽02021 小时前
C++之红黑树认识与实现
java·c++·rpc
百年孤独_1 小时前
LeetCode 算法题解:链表与二叉树相关问题 打打卡
算法·leetcode·链表
我爱C编程2 小时前
基于拓扑结构检测的LDPC稀疏校验矩阵高阶环检测算法matlab仿真
算法·matlab·矩阵·ldpc·环检测
算法_小学生2 小时前
LeetCode 75. 颜色分类(荷兰国旗问题)
算法·leetcode·职场和发展
运器1232 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
算法_小学生2 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode