200. 岛屿数量(一般) 给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
示例 1: 1 2 3 4 5 6 7 输入:grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] 输出:1
示例 2: 1 2 3 4 5 6 7 输入:grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] 输出:3
提示: 1 2 3 4 m == grid.length n == grid[i].length 1 <= m, n <= 300 grid[i] [j] 的值为 '0' 或 '1'
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/number-of-islands
思路1: DFS
基本思路
扫描整个网格,把找到的第一个位置为‘1’的节点作为起始节点,并开始深度优先搜索
在DFS的过程中,每个被找到的‘1’都会被重新标记为‘0’
DFS总结完就三大要素:终止条件,做选择 &下一层递归
作者:weichen-wang 链接:https://leetcode-cn.com/problems/number-of-islands/solution/dfs-bfs-unionfindbing-cha-ji-by-weichen-7ldae/
思路2: BFS
思路3: 并查集,遍历部分不是很理解
代码1&2: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 class Solution { public int numIslands (char [][] grid) { if (grid.length == 0 || grid == null ) { return 0 ; } int row = grid.length; int col = grid[0 ].length; int count = 0 ; for (int i=0 ;i<row;i++){ for (int j=0 ;j<col;j++){ if (grid[i][j]=='1' ){ count++; dfs(grid,i,j); } } } return count; } void dfs (char [][] grid,int row,int col) { if (row<0 || row>= grid.length || col<0 || col>= grid[0 ].length || grid[row][col]=='0' ){ return ; } grid[row][col]='0' ; dfs(grid,row-1 ,col); dfs(grid,row+1 ,col); dfs(grid,row,col-1 ); dfs(grid,row,col+1 ); } void bfs (char [][] grid,int row,int col) { Queue<int []> queue = new LinkedList<>(); queue.add(new int []{row,col}); while (!queue.isEmpty()){ int [] cur = queue.poll(); row = cur[0 ]; col = cur[1 ]; if (row-1 >=0 && grid[row-1 ][col] == '1' ){ queue.add(new int []{row-1 ,col}); grid[row-1 ][col]='0' ; } if (row+1 <grid.length && grid[row+1 ][col] == '1' ){ queue.add(new int []{row+1 ,col}); grid[row+1 ][col]='0' ; } if (col-1 >=0 && grid[row][col-1 ] == '1' ){ queue.add(new int []{row,col-1 }); grid[row][col-1 ]='0' ; } if (col+1 <grid[0 ].length && grid[row][col+1 ] == '1' ){ queue.add(new int []{row,col+1 }); grid[row][col+1 ]='0' ; } } } }
并查集 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 class Solution { public int numIslands (char [][] grid) { if (grid==null || grid.length==0 ){ return 0 ; } int row = grid.length; int col = grid[0 ].length; UnionFind uf = new UnionFind(row*col); int water = 0 ; for (int i=0 ;i<row;i++){ for (int j=0 ;j<col;j++){ if (grid[i][j] == '0' ) { water++; } else { int [][] directions = {{0 , 1 }, {0 , -1 }, {1 , 0 }, {-1 , 0 }}; for (int [] dir : directions) { int x = i + dir[0 ]; int y = j + dir[1 ]; if (x >= 0 && y >= 0 && x < row && y < col && grid[x][y] == '1' ) { uf.union(i * col + j, x * col + y); } } } } } return uf.count - water; } } class UnionFind { int [] parent; int count; public UnionFind (int n) { parent = new int [n]; count =n; for (int i=0 ;i<n;i++){ parent[i]=i; } } int find (int n) { while (parent[n]!=n){ parent[n]=parent[parent[n]]; n= parent[n]; } return n; } boolean isConnected (int x,int y) { return find(x)==find(y); } void union (int x,int y) { if (find(x)==find(y)){ return ; } parent[find(x)] = find(y); count--; } }