力扣200. 岛屿数量(BFS)

Problem: 200. 岛屿数量

题目描述

在这里插入图片描述在这里插入图片描述

思路及解法

1.定义方向数组:定义一个方向数组 DIRECTIONS,表示上、下、左、右四个方向的移动。
2.获取网格的行数和列数同时初始化一个计数器 numIslands 用于记录岛屿的数量。
3.使用两层循环遍历整个网格,如果遇到一个未访问的陆地 ‘1’,计数器 numIslands 增加1,并调用 BFS 方法来标记整个岛屿。
4.BFS方法:

4.1.创建一个队列 queue,并将当前陆地的位置加入队列。
4.2.将当前陆地标记为已访问,即将 grid[row][col] 设置为 ‘0’。
4.3.使用一个循环处理队列中的每个位置,遍历四个遍历四个方向,根据方向数组计算新位置。如果新位置是有效的且为未访问的陆地 ‘1’,将其加入队列并标记为已访问。

复杂度

时间复杂度:

O ( M × N ) O(M \times N) O(M×N);其中 M M M N N N分别为举证grid的行数与列数

空间复杂度:

O ( M × N ) O(M \times N) O(M×N)

Code

class Solution {
   // Define four directions: up, down, left, and right
    private static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    /**
     * Number of Islands
     *
     * @param grid Given array
     * @return int
     */
    public int numIslands(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int rows = grid.length;
        int cols = grid[0].length;
        int numIslands = 0;

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; col++) {
                if (grid[row][col] == '1') {
                    numIslands++;
                    bfs(grid, row, col);
                }
            }
        }

        return numIslands;
    }

    /**
     * @param grid Given array
     * @param row  The row of array
     * @param col  The column of array
     */
    private void bfs(char[][] grid, int row, int col) {
        int rows = grid.length;
        int cols = grid[0].length;
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{row, col});
        grid[row][col] = '0';  // Mark as accessed

        while (!queue.isEmpty()) {
            int[] current = queue.poll();
            int currentRow = current[0];
            int currentCol = current[1];

            for (int[] direction : DIRECTIONS) {
                int newRow = currentRow + direction[0];
                int newCol = currentCol + direction[1];

                if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols && grid[newRow][newCol] == '1') {
                    queue.offer(new int[]{newRow, newCol});
                    grid[newRow][newCol] = '0';  // Mark as accessed
                }
            }
        }
    }
}

相关推荐

  1. 200. 岛屿数量

    2024-06-06 01:58:01       52 阅读
  2. 200. 岛屿数量(Python3)

    2024-06-06 01:58:01       65 阅读
  3. 【宽度优先搜索 BFS】LeetCode-200. 岛屿数量

    2024-06-06 01:58:01       58 阅读
  4. 200. 岛屿数量

    2024-06-06 01:58:01       54 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 01:58:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 01:58:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 01:58:01       87 阅读
  4. Python语言-面向对象

    2024-06-06 01:58:01       96 阅读

热门阅读

  1. 视觉SLAM

    2024-06-06 01:58:01       27 阅读
  2. 003 Spring注解

    2024-06-06 01:58:01       18 阅读
  3. nuxt3 api如何透传(不引第3方库)

    2024-06-06 01:58:01       28 阅读
  4. Lisp解析器技术文档

    2024-06-06 01:58:01       20 阅读
  5. Django 默认 CSRF 保护机制

    2024-06-06 01:58:01       34 阅读
  6. C语言编译与链接

    2024-06-06 01:58:01       31 阅读
  7. 设计模式(简要,应付软考)

    2024-06-06 01:58:01       25 阅读
  8. 概率图模型在自然语言处理中的应用

    2024-06-06 01:58:01       25 阅读
  9. AWS与SAP扩大战略合作:通过AI增强ERP解决方案

    2024-06-06 01:58:01       32 阅读
  10. 6月01日,每日信息差

    2024-06-06 01:58:01       28 阅读