classSolution{voiddfs(char[][] grid,int r,int c){int nr = grid.length;int nc = grid[0].length;if(r <0|| c <0|| r >= nr || c >= nc || 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);}publicintnumIslands(char[][] grid){if(grid == null || grid.length ==0){return0;}int nr = grid.length;int nc = grid[0].length;int num_islands =0;for(int r =0; r < nr;++r){for(int c =0; c < nc;++c){if(grid[r][c]=='1'){++num_islands;dfs(grid, r, c);}}}return num_islands;}}
作者:LeetCode
链接:https://leetcode-cn.com/problems/number-of-islands/solution/dao-yu-shu-liang-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题解做法2:广度优先
classSolution{publicintnumIslands(char[][] grid){if(grid == null || grid.length ==0){return0;}int nr = grid.length;int nc = grid[0].length;int num_islands =0;for(int r =0; r < nr;++r){for(int c =0; c < nc;++c){if(grid[r][c]=='1'){++num_islands;
grid[r][c]='0';
Queue<Integer> neighbors =newLinkedList<>();
neighbors.add(r * nc + c);while(!neighbors.isEmpty()){int id = neighbors.remove();int row = id / nc;int col = id % nc;if(row -1>=0&& grid[row-1][col]=='1'){
neighbors.add((row-1)* nc + col);
grid[row-1][col]='0';}if(row +1< nr && grid[row+1][col]=='1'){
neighbors.add((row+1)* nc + col);
grid[row+1][col]='0';}if(col -1>=0&& grid[row][col-1]=='1'){
neighbors.add(row * nc + col-1);
grid[row][col-1]='0';}if(col +1< nc && grid[row][col+1]=='1'){
neighbors.add(row * nc + col+1);
grid[row][col+1]='0';}}}}}return num_islands;}}
作者:LeetCode
链接:https://leetcode-cn.com/problems/number-of-islands/solution/dao-yu-shu-liang-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题解做法3:并查集
classSolution{classUnionFind{int count;int[] parent;int[] rank;publicUnionFind(char[][] grid){
count =0;int m = grid.length;int n = grid[0].length;
parent =newint[m * n];
rank =newint[m * n];for(int i =0; i < m;++i){for(int j =0; j < n;++j){if(grid[i][j]=='1'){
parent[i * n + j]= i * n + j;++count;}
rank[i * n + j]=0;}}}publicintfind(int i){if(parent[i]!= i) parent[i]=find(parent[i]);return parent[i];}publicvoidunion(int x,int y){int rootx =find(x);int rooty =find(y);if(rootx != rooty){if(rank[rootx]> rank[rooty]){
parent[rooty]= rootx;}elseif(rank[rootx]< rank[rooty]){
parent[rootx]= rooty;}else{
parent[rooty]= rootx;
rank[rootx]+=1;}--count;}}publicintgetCount(){return count;}}publicintnumIslands(char[][] grid){if(grid == null || grid.length ==0){return0;}int nr = grid.length;int nc = grid[0].length;int num_islands =0;
UnionFind uf =newUnionFind(grid);for(int r =0; r < nr;++r){for(int c =0; c < nc;++c){if(grid[r][c]=='1'){
grid[r][c]='0';if(r -1>=0&& grid[r-1][c]=='1'){
uf.union(r * nc + c,(r-1)* nc + c);}if(r +1< nr && grid[r+1][c]=='1'){
uf.union(r * nc + c,(r+1)* nc + c);}if(c -1>=0&& grid[r][c-1]=='1'){
uf.union(r * nc + c, r * nc + c -1);}if(c +1< nc && grid[r][c+1]=='1'){
uf.union(r * nc + c, r * nc + c +1);}}}}return uf.getCount();}}
作者:LeetCode
链接:https://leetcode-cn.com/problems/number-of-islands/solution/dao-yu-shu-liang-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。