200. 岛屿数量_中等_不再记笔记了

 

 

感觉把题解法和思路直接写在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;    
    }
}

 

posted @ 2021-09-29 23:17  你的雷哥  阅读(94)  评论(0编辑  收藏  举报