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;
    }
};
相关推荐
疑惑的杰瑞22 分钟前
[数据结构]带头双向循环链表的实现与应用
c语言·数据结构·算法·链表
Liu_Junwei43 分钟前
回溯算法解决排列组合及子集问题
数据结构·算法
DdddJMs__1351 小时前
C语言 | Leetcode C语言题解之第454题四数相加II
c语言·leetcode·题解
xiao_fwuu1 小时前
LeetCode 120. 三角形最小路径和
算法·leetcode·职场和发展
黎明smaly2 小时前
【数据结构与算法初阶】前言介绍
c语言·开发语言·数据结构·算法
、达西先生2 小时前
ros中和RVIZ交互显示的InteractiveMarker简单例子
c++·ros·交互·rviz
机器视觉知识推荐、就业指导2 小时前
Qt/C++开源控件 自定义雷达控件
开发语言·c++·qt
single5942 小时前
【优选算法】(第十八篇)
java·数据结构·c++·vscode·算法·leetcode
视觉人机器视觉2 小时前
爸妈总说着学门技术,学机器视觉技术确实是一条踏实的生活道路,这条路你走得下去走得通吗?
人工智能·算法·计算机视觉·c#·自动化
学步_技术2 小时前
自动驾驶系列—从IMU到惯性定位算法:自动驾驶精准定位的幕后科技
科技·算法·自动驾驶·imu·惯性测量单元