感觉把题解法和思路直接写在LeetCode网站上就好了,写博客麻烦了。
class Solution {
int deleteIslands(int i,int j,char [][]grid,int [][]foot){
foot[i][j] =1;
if(grid[i][j] == '0') return 0;
else{
grid[i][j] = '0';
if(i<grid.length-1 && foot[i+1][j]==0){
deleteIslands(i+1,j, grid,foot);
}
if(j<grid[0].length-1 && foot[i][j+1]==0){
deleteIslands(i,j+1, grid,foot);
}
if(i>0 && foot[i-1][j]==0){
deleteIslands(i-1,j, grid,foot);
}
if(j>0 && foot[i][j-1]==0){
deleteIslands(i,j-1, grid,foot);
}
return 0;
}
}
public int numIslands(char[][] grid) {
int islandNum = 0;
int foot[][] = new int[grid.length][grid[0].length];
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]=='1'){
islandNum++;
deleteIslands(i,j,grid,foot);
}
}
}
return islandNum;
}
}
作者:你的雷哥
本文版权归作者所有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。