• [LeetCode] 200. Number of Islands


    Given an m x n 2d grid map of '1's (land) and '0's (water), return the number of islands.

    An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

    Example 1:

    Input: grid = [
      ["1","1","1","1","0"],
      ["1","1","0","1","0"],
      ["1","1","0","0","0"],
      ["0","0","0","0","0"]
    ]
    Output: 1
    

    Example 2:

    Input: grid = [
      ["1","1","0","0","0"],
      ["1","1","0","0","0"],
      ["0","0","1","0","0"],
      ["0","0","0","1","1"]
    ]
    Output: 3

    Constraints:

    • m == grid.length
    • n == grid[i].length
    • 1 <= m, n <= 300
    • grid[i][j] is '0' or '1'.

    岛屿数量。

    给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

    这一类flood fill的题的做法无非就是BFS和DFS。两种做法都要会。

    BFS

    时间O(mn)

    空间O(mn)

    Java实现

     1 class Solution {
     2     int rows;
     3     int cols;
     4 
     5     public int numIslands(char[][] grid) {
     6         int count = 0;
     7         for (int i = 0; i < grid.length; i++) {
     8             for (int j = 0; j < grid[0].length; j++) {
     9                 if (grid[i][j] == '1') {
    10                     bfs(grid, i, j);
    11                     count++;
    12                 }
    13             }
    14         }
    15         return count;
    16     }
    17 
    18     private void bfs(char[][] grid, int x, int y) {
    19         grid[x][y] = '0';
    20         int rows = grid.length;
    21         int cols = grid[0].length;
    22         Queue<Integer> queue = new LinkedList<>();
    23         int code = x * cols + y;
    24         queue.offer(code);
    25         while (!queue.isEmpty()) {
    26             code = queue.poll();
    27             int i = code / cols;
    28             int j = code % cols;
    29             if (i > 0 && grid[i - 1][j] == '1') {
    30                 queue.offer((i - 1) * cols + j);
    31                 grid[i - 1][j] = '0';
    32             }
    33             if (i < rows - 1 && grid[i + 1][j] == '1') {
    34                 queue.offer((i + 1) * cols + j);
    35                 grid[i + 1][j] = '0';
    36             }
    37             if (j > 0 && grid[i][j - 1] == '1') {
    38                 queue.offer((i * cols) + j - 1);
    39                 grid[i][j - 1] = '0';
    40             }
    41             if (j < cols - 1 && grid[i][j + 1] == '1') {
    42                 queue.offer((i * cols) + j + 1);
    43                 grid[i][j + 1] = '0';
    44             }
    45         }
    46     }
    47 }

    另一种Java实现,queue直接就是记录坐标,不需要convert成一个code

     1 class Solution {
     2     public int numIslands(char[][] grid) {
     3         int count = 0;
     4         for (int i = 0; i < grid.length; i++) {
     5             for (int j = 0; j < grid[0].length; j++) {
     6                 if (grid[i][j] == '1') {
     7                     count++;
     8                     bfs(grid, i, j);
     9                 }
    10             }
    11         }
    12         return count;
    13     }
    14 
    15     private void bfs(char[][] grid, int i, int j) {
    16         grid[i][j] = '0';
    17         int rows = grid.length;
    18         int cols = grid[0].length;
    19         Queue<int[]> queue = new LinkedList<>();
    20         queue.add(new int[] { i, j });
    21         while (!queue.isEmpty()) {
    22             int[] cur = queue.poll();
    23             i = cur[0];
    24             j = cur[1];
    25             if (i > 0 && grid[i - 1][j] == '1') {
    26                 queue.offer(new int[] { i - 1, j });
    27                 grid[i - 1][j] = '0';
    28             }
    29             if (j > 0 && grid[i][j - 1] == '1') {
    30                 queue.offer(new int[] { i, j - 1 });
    31                 grid[i][j - 1] = '0';
    32             }
    33             if (i < rows - 1 && grid[i + 1][j] == '1') {
    34                 queue.offer(new int[] { i + 1, j });
    35                 grid[i + 1][j] = '0';
    36             }
    37             if (j < cols - 1 && grid[i][j + 1] == '1') {
    38                 queue.offer(new int[] { i, j + 1 });
    39                 grid[i][j + 1] = '0';
    40             }
    41         }
    42     }
    43 }

    JavaScript实现

     1 /**
     2  * @param {character[][]} grid
     3  * @return {number}
     4  */
     5 var numIslands = function(grid) {
     6     let res = 0;
     7 
     8     var bfs = function(grid, x, y) {
     9         grid[x][y] = '0';
    10         let rows = grid.length;
    11         let cols = grid[0].length;
    12         let queue = [];
    13         let code = x * cols + y;
    14         queue.push(code);
    15         while (queue.length) {
    16             code = queue.shift();
    17             let i = parseInt(code / cols);
    18             let j = code % cols;
    19             if (i > 0 && grid[i - 1][j] == '1') {
    20                 queue.push((i - 1) * cols + j);
    21                 grid[i - 1][j] = '0';
    22             }
    23             if (i < rows - 1 && grid[i + 1][j] == '1') {
    24                 queue.push((i + 1) * cols + j);
    25                 grid[i + 1][j] = '0';
    26             }
    27             if (j > 0 && grid[i][j - 1] == '1') {
    28                 queue.push(i * cols + j - 1);
    29                 grid[i][j - 1] = '0';
    30             }
    31             if (j < cols - 1 && grid[i][j + 1] == '1') {
    32                 queue.push(i * cols + j + 1);
    33                 grid[i][j + 1] = '0';
    34             }
    35         }
    36     };
    37 
    38     for (let i = 0; i < grid.length; i++) {
    39         for (let j = 0; j < grid[0].length; j++) {
    40             if (grid[i][j] === '1') {
    41                 bfs(grid, i, j);
    42                 res++;
    43             }
    44         }
    45     }
    46     return res;
    47 };

    DFS

    时间O(mn)

    空间O(n)

    Java实现

     1 class Solution {
     2     private int rows;
     3     private int cols;
     4 
     5     public int numIslands(char[][] grid) {
     6         int count = 0;
     7         rows = grid.length;
     8         if (rows == 0) {
     9             return 0;
    10         }
    11         cols = grid[0].length;
    12         for (int i = 0; i < rows; i++) {
    13             for (int j = 0; j < cols; j++) {
    14                 if (grid[i][j] == '1') {
    15                     dfs(grid, i, j);
    16                     count++;
    17                 }
    18             }
    19         }
    20         return count;
    21     }
    22 
    23     private void dfs(char[][] grid, int i, int j) {
    24         if (i < 0 || j < 0 || i >= rows || j >= cols || grid[i][j] != '1') {
    25             return;
    26         }
    27         grid[i][j] = '0';
    28         dfs(grid, i + 1, j);
    29         dfs(grid, i - 1, j);
    30         dfs(grid, i, j + 1);
    31         dfs(grid, i, j - 1);
    32     }
    33 }

    JavaScript实现

     1 /**
     2  * @param {character[][]} grid
     3  * @return {number}
     4  */
     5 var numIslands = function(grid) {
     6     let res = 0;
     7     let rows = grid.length;
     8     if (rows == 0) return 0;
     9     let cols = grid[0].length;
    10 
    11     var dfs = function(grid, i, j) {
    12         if (i < 0 || j < 0 || i >= rows || j >= cols || grid[i][j] == '0') {
    13             return;
    14         }
    15         grid[i][j] = '0';
    16         dfs(grid, i, j + 1);
    17         dfs(grid, i, j - 1);
    18         dfs(grid, i - 1, j);
    19         dfs(grid, i + 1, j);
    20     };
    21 
    22     for (let i = 0; i < rows; i++) {
    23         for (let j = 0; j < cols; j++) {
    24             if (grid[i][j] == '1') {
    25                 dfs(grid, i, j);
    26                 res++;
    27             }
    28         }
    29     }
    30     return res;
    31 };

    flood fill题型总结

    LeetCode 题目总结

  • 相关阅读:
    Windows 黑屏问题
    Java原生API操作XML
    Java使用Schema模式对XML验证
    使用Spring构建RMI服务器和客户端
    Eclipse多个console的使用
    jsr133-第一二章
    装个centos虚拟机之设置桥接网络
    Hadoop学习
    关于配置中心选型
    maven的SNAPSHOT版本和正式版本不同
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12652761.html
Copyright © 2020-2023  润新知