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;
    }
};
相关推荐
MIUMIUKK22 分钟前
双指针三大例题
算法
灵感__idea23 分钟前
Hello 算法:复杂问题的应对策略
前端·javascript·算法
2301_819414301 小时前
C++与区块链智能合约
开发语言·c++·算法
Zaly.1 小时前
【Python刷题】LeetCode 1727 重新排列后的最大子矩阵
算法·leetcode·矩阵
不想看见4041 小时前
Valid Parentheses栈和队列--力扣101算法题解笔记
开发语言·数据结构·c++
老约家的可汗2 小时前
C/C++内存管理探秘:从内存分布到new/delete的底层原理
c语言·c++
做怪小疯子2 小时前
蚂蚁暑期 319 笔试
算法·职场和发展
天赐学c语言2 小时前
Linux - 应用层自定义协议与序列/反序列化
linux·服务器·网络·c++
计算机安禾2 小时前
【C语言程序设计】第37篇:链表数据结构(一):单向链表的实现
c语言·开发语言·数据结构·c++·算法·链表·蓝桥杯
啊哦呃咦唔鱼2 小时前
LeetCode hot100-73 矩阵置零
算法