200. Number of Islands

class Solution {
    public int numIslands(char[][] grid) {
        int count=0;
        for(int i=0;i<grid.length;i++)
            for(int j=0;j<grid[0].length;j++)
            {
                if(grid[i][j]=='1')
                {
                    fill(i, j, grid);
                    count++;
                }
            }
        return count;
    }
    private void fill(int i, int j, char[][] grid){
        if(i<0||i>=grid.length||j<0||j>=grid[0].length||grid[i][j]!='1')
            return;
        grid[i][j]='2';
        fill(i-1, j, grid);
        fill(i+1, j, grid);
        fill(i, j-1, grid);
        fill(i, j+1, grid);
    }
}

  

转载于:https://www.cnblogs.com/asuran/p/7734152.html

上一篇:IOI Islands


下一篇:200. Number of Islands