200. Number of Islands + 695. Max Area of Island

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input:
11110
11010
11000
00000

Output: 1
Example 2:

Input:
11000
11000
00100
00011

Output: 3

Solution: dfs or bfs or union find

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(grid[i][j] == '1'){
                    res++;
                    dfs(grid, i, j);
                }
            }
        }
        return res;
    }
    void dfs(char[][] grid, int i, int j){
        if(i >= grid.length || i<0 || j>=grid[0].length || j < 0 || grid[i][j] == '0') return;
        grid[i][j] = '0'; // visited
        dfs(grid, i-1, j);
        dfs(grid, i+1, j);
        dfs(grid, i, j-1);
        dfs(grid, i, j+1);
    }
}

 

FOllow up get max number of islands

695

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

Example 1:
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,1,1,0,1,0,0,0,0,0,0,0,0],
 [0,1,0,0,1,1,0,0,1,0,1,0,0],
 [0,1,0,0,1,1,0,0,1,1,1,0,0],
 [0,0,0,0,0,0,0,0,0,0,1,0,0],
 [0,0,0,0,0,0,0,1,1,1,0,0,0],
 [0,0,0,0,0,0,0,1,1,0,0,0,0]]
Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
Example 2:
[[0,0,0,0,0,0,0,0]]
Given the above grid, return 0.
Note: The length of each dimension in the given grid does not exceed 50.

SOlution dfs + array to store the number

class Solution {
    int[] a;
    public int maxAreaOfIsland(int[][] grid) {
        a = new int[grid.length*grid[0].length];
        int res = 0;
        for(int i=0; i<grid.length; i++){
            for(int j = 0; j<grid[0].length; j++){
                if(grid[i][j] == 1){
                    dfs(grid, i, j, res);
                    res++;
                }
            }
        }
        int max = 0;
        for(int i = 0; i<res; i++){
            //System.out.println(a[i]);
            if(max < a[i]) max = a[i];
        }
        return max;
    }
    void dfs(int[][] grid, int i, int j,int res){
        if(i >= grid.length || i<0 || j>=grid[0].length || j < 0 || grid[i][j] == 0) return;
        a[res]++;
        grid[i][j] = 0; // visited
        dfs(grid, i-1, j, res);
        dfs(grid, i+1, j, res);
        dfs(grid, i, j-1, res);
        dfs(grid, i, j+1, res);
    }
}

There is another aolution with dfs without array ....

class Solution {
    int[][] grid;
    boolean[][] seen;

    public int area(int r, int c) {
        if (r < 0 || r >= grid.length || c < 0 || c >= grid[0].length ||
                seen[r][c] || grid[r][c] == 0)
            return 0;
        seen[r][c] = true;
        return (1 + area(r+1, c) + area(r-1, c)
                  + area(r, c-1) + area(r, c+1));
    }

    public int maxAreaOfIsland(int[][] grid) {
        this.grid = grid;
        seen = new boolean[grid.length][grid[0].length];
        int ans = 0;
        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid[0].length; c++) {
                ans = Math.max(ans, area(r, c));
            }
        }
        return ans;
    }
}
posted @ 2018-08-14 08:58  wz30  阅读(223)  评论(0编辑  收藏  举报