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

  

posted @ 2017-10-26 01:48  Weiyu Wang  阅读(119)  评论(0编辑  收藏  举报