My LeetCode: https://leetcode-cn.com/u/ituring/
My LeetCode source code [GitHub]: https://github.com/izhoujie/Algorithmcii
LeetCode 200. Number of islands
subject
Give you a two-dimensional grid consisting of '1' (land) and '0' (water). Please calculate the number of islands in the grid.
Islands are always surrounded by water, and each island can only be formed by adjacent land connections in the horizontal and / or vertical directions.
In addition, you can assume that all four sides of the mesh are surrounded by water.
Example 1:
Input: 11110 11010 11000 00000 Output: 1
Example 2:
Input: 11000 11000 00100 00011 Output: 3 Explanation: each island can only be connected by adjacent land in horizontal and / or vertical directions.
Source: LeetCode
Link: https://leetcode-cn.com/problems/number-of-islands
Copyright belongs to the network. For commercial reprint, please contact the official authorization. For non-commercial reprint, please indicate the source.
Solutions to problems
Naturally, we can think of using dfs to solve the problem, which is more convenient. We can also use bfs. Each 1 needs bfs once;
Idea 1-dfs numbering Islands
Every time a 1 is encountered, dfs is used, and the number starting from '2' is given. After traversing the last number, subtract '2', that is, the number of islands;
Algorithm complexity:
- M is the number of rows and N is the number of columns;
- Time complexity: ${\ color{Magenta}{\Omicron\left(M \ast N\right)}}}$
- Spatial complexity: ${\ color{Magenta}{\Omicron\left(M \ast N\right)}}}$
Idea 2-bfs numbering Islands
bfs is used for each 1, and the number starting from '2' is given. After traversing the last number, subtract '2', that is, the number of islands;
Tips: before bfs encountered data such as arrays, it is necessary to use arrays to store two-dimensional coordinates when constructing queues for storage, but this topic transforms coordinates into int storage
- row is the number of rows and col is the number of columns. For coordinates [i,j], t=i*col+j, store T;
- Because J < col, i=t/col, j=t%col. thus, the time-consuming operation of array creation can be avoided by compressing two dimensions into one dimension;
Algorithm complexity:
- M is the number of rows and N is the number of columns;
- Time complexity: ${\ color{Magenta}{\Omicron\left(M \ast N\right)}}}$
- Spatial complexity: ${\ color{Magenta}{\Omicron\left(Min\left(M,N\right)\right)}}}$
Algorithm source code example
package leetcode; import java.util.Deque; import java.util.LinkedList; /** * @author ZhouJie * @date 2020 3:23:26 PM, April 20, 2010 * @Description: 200. Number of islands * */ public class LeetCode_0200 { } class Solution_0200 { /** * @author: ZhouJie * @date: 2020 April 20, 2004 4:12:48 PM * @param: @param grid * @param: @return * @return: int * @Description: 1-DFS,Every time a 1 is found, it will give a number to the island and DFS, and the last named number is the number of islands; * */ public int numIslands_1(char[][] grid) { if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; } int row = grid.length; int col = grid[0].length; // Island number starts from '2' because the source array is' 1 ' char count = '2'; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { // Every time a '1' is found, it will be dfs and marked with the current number if (grid[i][j] == '1') { dfs(grid, i, j, count); // After marking, the current island number is incremented count++; } } } // Total number of islands marked return count - '2'; } /** * @author: ZhouJie * @date: 2020 4:13:59 p.m., April 20, 2010 * @param: @param grid * @param: @param i * @param: @param j * @param: @param count * @return: void * @Description: dfs * */ private void dfs(char[][] grid, int i, int j, char count) { if (i >= 0 && i < grid.length && j >= 0 && j < grid[0].length && grid[i][j] == '1') { grid[i][j] = count; dfs(grid, i - 1, j, count); dfs(grid, i + 1, j, count); dfs(grid, i, j - 1, count); dfs(grid, i, j + 1, count); } } /** * @author: ZhouJie * @date: 2020 April 20, 2004 4:22:47 PM * @param: @param grid * @param: @return * @return: int * @Description: 2-BFS,Different and homologous BFS, the problem is to calculate the number of different homologies, so every '1' needs a BFS; * */ public int numIslands_2(char[][] grid) { if (grid == null || grid.length == 0 || grid[0].length == 0) { return 0; } int row = grid.length; int col = grid[0].length; // Island number starts from '2' because the source array is' 1 ' char count = '2'; Deque<Integer> queue = new LinkedList<Integer>(); for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { // Every time a '1' is found, it will be dfs and marked with the current number if (grid[i][j] == '1') { // Here, the position of i j is very clever. Multiply i by Col and add J. because J < col, i can be obtained by dividing col when retrieving, and J can be obtained by moduling col, avoiding the use of array to store ij queue.offer(i * col + j); while (!queue.isEmpty()) { Integer poll = queue.poll(); int x = poll / col; int y = poll % col; if (x > 0 && grid[x - 1][y] == '1') { grid[x - 1][y] = count; queue.offer((x - 1) * col + y); } if (y > 0 && grid[x][y - 1] == '1') { grid[x][y - 1] = count; queue.offer(x * col + y - 1); } if (x < row - 1 && grid[x + 1][y] == '1') { grid[x + 1][y] = count; queue.offer((x + 1) * col + y); } if (y < col - 1 && grid[x][y + 1] == '1') { grid[x][y + 1] = count; queue.offer(x * col + y + 1); } } // After marking, the current island number is incremented count++; } } } return count - '2'; } }