Woodyiiiiiii / LeetCode

My private record of Leetcode solution

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LeetCode 200. Number of Islands

Woodyiiiiiii opened this issue · comments

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

这道题的做法是DFS和BFS为什么我一开始想不到:(

DFS建立一个二维访问数组v,遍历原数组,对原数组每个非'0'元素的四个方向作深度优先遍历。BFS同样遍历数组,但不同的是对每个非'0'元素建立一个队列,也是对四个方向遍历,符合条件的加入队列,循环直至队列为空。

DFS代码如下:

class Solution {
    public int numIslands(char[][] grid) {
        if (grid.length == 0) return 0;
        int m = grid.length, n = grid[0].length;
        int max = 0;
        int[][] v = new int[m][n];
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (grid[i][j] == '1') {
                    if (v[i][j] == 0) 
                        ++max;
                    dfs(v, grid, i, j);   
                }
            }
        }
        return max;
    }
    public void dfs(int[][] v, char[][] grid, int i, int j) {
        if (v[i][j] == 1) 
            return;
        v[i][j] = 1;
        if (i - 1 >= 0 && grid[i - 1][j] == '1') {
            dfs(v, grid, i - 1, j);
        }
        if (j - 1 >= 0 && grid[i][j - 1] == '1') {
            dfs(v, grid, i, j - 1);
        }
        if (i + 1 < grid.length && grid[i + 1][j] == '1') {
            dfs(v, grid, i + 1, j);
        }
        if (j + 1 < grid[0].length && grid[i][j + 1] == '1') {
            dfs(v, grid, i, j + 1);
        }
    } 
}

bfs代码如下:

class Solution {
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};
    public boolean check(int x, int y, char[][]g){
        if( x < 0 || y < 0 || x >= g.length || y >= g[0].length || g[x][y] == '0')
            return false;
        return true;
    }
    public void find(int i, int j,  char[][]g, boolean [][]f){  
        Queue<int[]> q = new LinkedList();
        q.add(new int[]{i, j});
        f[i][j] = true;
        while( ! q.isEmpty() ){
            int u[] = q.poll();
            for(int k = 0; k<4; k++){
                int x = u[0] + dx[k];
                int y = u[1] + dy[k];
                if( check(x, y, g) && !f[x][y] ){
                    f[x][y] = true;
                    q.add(new int[]{x, y});
                }
            }
        }

    }
    public int numIslands(char[][] grid) {
        if( grid.length == 0)   return 0;
        boolean [][]f = new boolean[grid.length][grid[0].length];
        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' && ! f[i][j] ){
                    count++;
                    find(i ,j, grid, f);
                }
            }
        }
        return count;   
    }
}

参考资料: