class Solution {
public int numIslands(char[][] grid) {
int res = 0;
for(int i = 0; i< grid.length;i++){
for(int j = 0; j<grid[0].length;j++){
if(dfs(grid,i,j)){
res+=1;
}
}
}
return res;
}
public boolean dfs(char [][]grid,int x,int y){
if(x<0||x>grid.length-1||y<0||y>grid[0].length-1||grid[x][y] == '0'){
return false;
}
grid[x][y] = '0';
dfs(grid,x,y+1);
dfs(grid,x,y-1);
dfs(grid,x+1,y);
dfs(grid,x-1,y);
return true;
}
}
DFS Leetcode 200
最新推荐文章于 2024-09-05 10:48:32 发布