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;
    }
};
相关推荐
卡洛斯(编程版14 分钟前
(1) 哈希表全思路-20天刷完Leetcode Hot 100计划
python·算法·leetcode
疯狂的代M夫15 分钟前
C++对象的内存布局
开发语言·c++
NAGNIP1 小时前
DeepSeekMoE 架构解析
算法
重启的码农1 小时前
llama.cpp 分布式推理介绍(4) RPC 服务器 (rpc_server)
c++·人工智能·神经网络
重启的码农1 小时前
llama.cpp 分布式推理介绍(3) 远程过程调用后端 (RPC Backend)
c++·人工智能·神经网络
不喜欢学数学er1 小时前
算法第五十二天:图论part03(第十一章)
算法·深度优先·图论
养成系小王1 小时前
四大常用排序算法
数据结构·算法·排序算法
NAGNIP1 小时前
一文搞懂DeepSeek LLM
算法
敲上瘾1 小时前
Linux I/O 多路复用实战:Select/Poll 编程指南
linux·服务器·c语言·c++·select·tcp·poll
已读不回1431 小时前
设计模式-策略模式
前端·算法·设计模式