[LeetCode] 200. Number of Islands
Given an m x n
2D binary grid grid
which represents a 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'(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这一类 flood fill 的题的做法无非就是 BFS 和 DFS。两种做法都要会。
BFS
时间O(mn)
空间O(mn)
Java实现
1 class Solution { 2 int rows; 3 int cols; 4 int[] dx = { 1, 0, 0, -1 }; 5 int[] dy = { 0, 1, -1, 0 }; 6 7 public int numIslands(char[][] grid) { 8 int count = 0; 9 rows = grid.length; 10 cols = grid[0].length; 11 for (int i = 0; i < rows; i++) { 12 for (int j = 0; j < cols; j++) { 13 if (grid[i][j] == '1') { 14 bfs(grid, i, j); 15 count++; 16 } 17 } 18 } 19 return count; 20 } 21 22 private void bfs(char[][] grid, int i, int j) { 23 grid[i][j] = '0'; 24 Queue<int[]> queue = new LinkedList<>(); 25 queue.offer(new int[] { i, j }); 26 while (!queue.isEmpty()) { 27 int[] cur = queue.poll(); 28 int x = cur[0]; 29 int y = cur[1]; 30 for (int k = 0; k < 4; k++) { 31 int newX = x + dx[k]; 32 int newY = y + dy[k]; 33 if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && grid[newX][newY] == '1') { 34 grid[newX][newY] = '0'; 35 queue.offer(new int[] { newX, newY }); 36 } 37 } 38 } 39 } 40 }
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 };