Niukenet Brush Questions-Number of Islands

Problem Description

Given a 01 matrix, 1 represents land and 0 represents ocean. If two 1s are adjacent, then these two 1s belong to the same island. We only consider up, down, left and right as adjacent.
Islands: Adjacent land can form an island (adjacent: up, down, left, and right) to determine the number of islands.

Example

Example 1

Enter
[[1,1,0,0,0],[0,1,0,1,1],[0,0,0,1,1],[0,0,0,0,0], [0,0,1,1,1]]

Output
3

Solutions

analysis

  1. Traverse the array, set the adjacent islands to 1 (you can use dfs or bfs), and then continue to judge.

method

  1. dfs: start from a root node of 1, visit from each adjacent 1 node down to the vertex (all around it is water), and visit other adjacent 1 nodes to the vertex in turn
  2. bfs: To find the number of islands, we can scan the entire two-dimensional grid. If a position is 1, it is added to the queue and the breadth first search is started. In the breadth-first search process, each searched 1 will be re-marked as 0. Until the queue is empty, the search ends.

Code

dfs

// 思路1
public class Solution {
    
      
    void dfs(char[][] grid, int r, int c) {
    
    
        int len = grid.length;
        int len1 = grid[0].length;

        if (r < 0 || c < 0 || r >= len || c >= len1 || grid[r][c] == '0') {
    
    
            return;
        }

        grid[r][c] = '0';
        dfs(grid, r - 1, c);
        dfs(grid, r + 1, c);
        dfs(grid, r, c - 1);
        dfs(grid, r, c + 1);
    }

    public int numIslandsByDFS(char[][] grid) {
    
    
        if (grid == null || grid.length == 0) {
    
    
            return 0;
        }

        int len = grid.length;
        int len1 = grid[0].length;
        int numIslands = 0;
        for (int r = 0; r < len; ++r) {
    
    
            for (int c = 0; c < len1; ++c) {
    
    
                if (grid[r][c] == '1') {
    
    
                    ++numIslands;
                    dfs(grid, r, c);
                }
            }
        }

        return numIslands;
    }
}

Time complexity analysis:
O(MN): Traverse a two-dimensional array

Space complexity analysis:
O(MN): The space complexity here is mainly for the depth of recursion. In the worst case, if the two-dimensional array is all 1, you need to recurse the entire two-dimensional array all the time.

bfs

// 思路2
public class Solution {
    
      
    public int numIslandsByBFS(char[][] grid) {
    
    
        if (grid == null || grid.length == 0) {
    
    
            return 0;
        }

        int len = grid.length;
        int len1 = grid[0].length;
        int numIslands = 0;

        for (int r = 0; r < len; ++r) {
    
    
            for (int c = 0; c < len1; ++c) {
    
    
                if (grid[r][c] == '1') {
    
    
                    ++numIslands;
                    grid[r][c] = '0';
                    Queue<Integer> neighbors = new LinkedList<>();
                    neighbors.add(r * len1 + c);
                    while (!neighbors.isEmpty()) {
    
    
                        int id = neighbors.remove();
                        int row = id / len1;
                        int col = id % len1;
                        if (row - 1 >= 0 && grid[row - 1][col] == '1') {
    
    
                            neighbors.add((row - 1) * len1 + col);
                            grid[row - 1][col] = '0';
                        }
                        if (row + 1 < len && grid[row + 1][col] == '1') {
    
    
                            neighbors.add((row + 1) * len1 + col);
                            grid[row + 1][col] = '0';
                        }
                        if (col - 1 >= 0 && grid[row][col - 1] == '1') {
    
    
                            neighbors.add(row * len1 + col - 1);
                            grid[row][col - 1] = '0';
                        }
                        if (col + 1 < len1 && grid[row][col + 1] == '1') {
    
    
                            neighbors.add(row * len1 + col + 1);
                            grid[row][col + 1] = '0';
                        }
                    }
                }
            }
        }

        return numIslands;
    }
} 

Time complexity analysis:
O(MN): Traverse a two-dimensional array

Space complexity analysis:
O(min(M,N)): In the worst case, the entire grid is land, and the size of the queue can reach min(M,N).

If you want to test, you can go directly to the link of Niuke.com to do the test

Number of islands-niuke.com

Guess you like

Origin blog.csdn.net/qq_35398517/article/details/114005320