其他分享
首页 > 其他分享> > 200. 岛屿数量_中等_不再记笔记了

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;    
    }
}

 

标签:200,int,中等,char,grid,&&,foot,deleteIslands,记笔记
来源: https://www.cnblogs.com/henuliulei/p/15354888.html