200. 岛屿数量-Java

文章目录

      • [200. 岛屿数量](https://leetcode-cn.com/problems/number-of-islands/)
        • 题目概述:
        • 算法思路:
          • 1. 深度优先搜索
        • 代码实现:
        • 复杂度分析:
          • 2. 广度优先搜索
          • 分离行与列的方法:
        • 代码实现:
        • 复杂度分析:

200. 岛屿数量

题目概述:

给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。

岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

此外,你可以假设该网格的四条边均被水包围。

 

示例 1:

输入:grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
输出:1
示例 2:

输入:grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
输出:3
 

提示:

	m == grid.length
	n == grid[i].length
	1 <= m, n <= 300
	grid[i][j] 的值为 '0' 或 '1'


算法思路:

1. 深度优先搜索
  • 求岛屿数量,与搜索相关。由题中所给条件:岛屿总是被水包围,可以得出DFS的结束条件:grid[row][column]=‘0’
  • 依次遍历grid中的所有元素,并且进行DFS,遇到陆地时,计数器自增
  • DFS中,遇到陆地(‘1’)就将陆地覆盖为水(‘0’),出边界以及遇到水时跳出搜索

代码实现:

 public void dfs(char[][] grid,int row,int column){
        int rowLength = grid.length;
        int columnLength = grid[0].length;
        if(row >= rowLength || column >= columnLength || row < 0 || column < 0 || grid[row][column] == '0'){
            return;
        }
        grid[row][column] = '0';
        dfs(grid,row - 1,column);
        dfs(grid,row + 1,column);
        dfs(grid,row,column + 1);
        dfs(grid,row,column - 1);
    }
    public int numIslands(char[][] grid) {
        if(grid == null || grid.length == 0){
            return -1;
        }
        int rowLength = grid.length;
        int columnLength = grid[0].length;
        int count = 0;

        for(int row = 0; row < rowLength; row++){
            for(int column = 0; column < columnLength; column++){
                if(grid[row][column] == '1'){
                    count++;
                }
                dfs(grid,row,column);
            }
        }
        return count;
    }

复杂度分析:

  • 时间复杂度:O(M*N),其中M和N分别为行数与列数
  • 空间复杂度:O(M*N),在最坏情况下,整个网格都为陆地,深度优先搜索的深度达到M*N
2. 广度优先搜索
  • 逻辑与深度优先搜索相似,当找到一个陆地时,计数器自增
  • 然后进行广度优先搜索,将附近的陆地全部更新为水
  • 当所有陆地都为水的时候,循环结束
分离行与列的方法:
a b c 三个数,需要将ab组合后再分离
组合:a*c + b
分离:(a*c + b)/c = a + 0
	(a*c + b)%c = 0 + b

代码实现:

public int numIslands(char[][] grid) {
        if(grid == null || grid.length == 0){
            return 0;
        }
        int rowLength = grid.length;
        int columnLength = grid[0].length;
        int count = 0;

        for(int row = 0; row < rowLength; row++){
            for(int column = 0; column < columnLength; column++) {
                if (grid[row][column] == '1') {
                    count++;
                    grid[row][column] = '0';
                    Queue<Integer> roundLand = new LinkedList<>();
                    roundLand.add(row * columnLength + column);
                    while (!roundLand.isEmpty()) {
                        int location = roundLand.remove();
                        int curRow = location / columnLength;
                        int curCol = location % columnLength;
                        if (curRow - 1 >= 0 && grid[curRow - 1][curCol] == '1') {
                            roundLand.add((curRow - 1) * columnLength + curCol);
                            grid[curRow - 1][curCol] = '0';
                        }
                        if (curRow + 1 < rowLength && grid[curRow + 1][curCol] == '1') {
                            roundLand.add((curRow + 1) * columnLength + curCol);
                            grid[curRow + 1][curCol] = '0';
                        }
                        if (curCol - 1 >= 0 && grid[curRow][curCol - 1] == '1') {
                            roundLand.add(curRow * columnLength + curCol - 1);
                            grid[curRow][curCol - 1] = '0';
                        }
                        if (curCol + 1 < columnLength && grid[curRow][curCol + 1] == '1') {
                            roundLand.add(curRow * columnLength + curCol + 1);
                            grid[curRow][curCol + 1] = '0';
                        }
                    }
                }
            }
        }
        return count;
}

复杂度分析:

  • 时间复杂度:O(MN),其中 MM 和 NN 分别为行数和列数。

  • 空间复杂度:O(min(M,N)),在最坏情况下,整个网格均为陆地,队列的大小可以达到 min(M,N)

你可能感兴趣的:(LeetCode,java,leetcode,深度优先)