leetcode200 岛屿数量(经典面试题)

leetcode 200 岛屿数量

leetcode 200 岛屿数量

给你一个由 ‘1’(陆地)和 ‘0’(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。

示例 1:
输入:grid = [
[“1”,“1”,“1”,“1”,“0”],
[“1”,“1”,“0”,“1”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“0”,“0”,“0”]
]
输出:1

示例 2:
输入:grid = [
[“1”,“1”,“0”,“0”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“1”,“0”,“0”],
[“0”,“0”,“0”,“1”,“1”]
]
输出:3

提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] 的值为 ‘0’ 或 ‘1’
解答1:

//采用 dfs 的方法求解
public class Solution {
    
    
    public int numIslands(char[][] grid) {
    
    
        //边界判断 grid 是空或者长度为0 那么直接返回 0
        if (grid == null || grid.length == 0)
            return 0;
        // 得出行列信息 和统计结果信息
        int row = grid.length;
        int col = grid[0].length;
        int res = 0;
        //循环遍历
        for (int i = 0; i < row; i++) {
    
    
            for (int j = 0; j < col; j++) {
    
    
                if (grid[i][j] == '1') {
    
    
                    res += 1;
                    dfs(grid, i, j, row, col);
                }
            }
        }
        return res;
    }

    private void dfs(char[][] grid, int x, int y, int row, int col) {
    
    
        //递归终止条件
        if (x < 0 || y < 0 || x >= row || y >= col || grid[x][y] == '0')
            return;
        grid[x][y] = '0';
        dfs(grid, x - 1, y, row, col);
        dfs(grid, x + 1, y, row, col);
        dfs(grid, x, y - 1, row, col);
        dfs(grid, x, y + 1, row, col);
    }
}

解答2:

//采用bfs的方法解决,需要维护一个队列
class Solution {
    
    
    public int numIslands(char[][] grid) {
    
    
        // 边界条件判断
        if (grid == null || grid.length == 0) return 0;
        // 行列计算
        int row = grid.length;
        int col = grid[0].length;
        int res = 0;
        //维护一个队列 , 里边存放的是 当值为'1' 是当二维数组索引 ,数组类型
        Queue<int[]> queue = new LinkedList<>();
        for (int i = 0; i < row; i++) {
    
    
            for (int j = 0; j < col; j++) {
    
    
                if (grid[i][j] == '1') {
    
    
                    res += 1;
                    queue.add(new int[]{
    
    i, j});
                    grid[i][j] = '0';
                    while (queue.size() > 0) {
    
    
                        int[] cur = queue.remove();
                        int x = cur[0];
                        int y = cur[1];
                        //进行上下左右判断,同化操作
                        if (x - 1 >= 0 && grid[x - 1][y] == '1') {
    
    
                            queue.add(new int[]{
    
    x - 1, y});
                            grid[x - 1][y] = '0';
                        }
                        if (y - 1 >= 0 && grid[x][y - 1] == '1') {
    
    
                            queue.add(new int[]{
    
    x, y - 1});
                            grid[x][y - 1] = '0';
                        }
                        if (x + 1 < row && grid[x + 1][y] == '1') {
    
    
                            queue.add(new int[]{
    
    x + 1, y});
                            grid[x + 1][y] = '0';
                        }
                        if (y + 1 < col && grid[x][y + 1] == '1') {
    
    
                            queue.add(new int[]{
    
    x, y + 1});
                            grid[x][y + 1] = '0';
                        }
                    }
                }
            }
        }
        return res;
    }
}

解答3:

// 采用并查集的方法求解,涉及二维数组-> 一维数组索引的变换
class Solution {
    
    
    public int numIslands(char[][] grid) {
    
    
        if (grid == null || grid.length == 0) {
    
    
            return 0;
        }
        int row = grid.length;
        int col = grid[0].length;
        int waters = 0;
        UnionFind uf = new UnionFind(grid);
        for (int i = 0; i < row; i++) {
    
    
            for (int j = 0; j < col; j++) {
    
    
                if (grid[i][j] == '0') {
    
    
                    waters++;
                } else {
    
    
                    int[][] directions = new int[][]{
    
    {
    
    0,1}, {
    
    0, -1}, {
    
    1, 0}, {
    
    -1, 0}};
                    for (int[] dir : directions) {
    
    
                        int x = i + dir[0];
                        int y = j + dir[1];
                        if (x >= 0 && y >= 0 && x < row && y < col && grid[x][y] == '1') {
    
    
                            uf.union(x*col+y, i*col+j);
                        }
                    }
                }
            }
        }
        return uf.getCount() - waters;
    }
}

class UnionFind {
    
    
    private int[] root = null;
    private int count = 0;
    
    public UnionFind(char[][] grid) {
    
    
        int row = grid.length;
        int col = grid[0].length;
        count = row * col;
        root = new int[row*col];
        for(int i = 0; i < row*col; i++) {
    
    
            root[i] = i;
        }
    }

    // Find the root of X
    public int find(int x) {
    
    
        if (x == root[x]) {
    
    
            return x;
        }
        return root[x] = find(root[x]);
    }

    // Union two element into one root
    public void union(int x, int y) {
    
    
        int rootX = find(x);
        int rootY = find(y);
        if (rootX != rootY) {
    
    
            root[rootX] = rootY;
            count--;
        }
    }

    public int getCount() {
    
    
        return count;
    }
}

推广

云数据库RDS MySQL 版是全球最受欢迎的开源数据库之一,作为开源软件组合 LAMP(Linux + Apache + MySQL + Perl/PHP/Python) 中的重要一环,广泛应用于各类应用场景。
点击查看->>> 云数据库新年钜惠,新用户1折起,爆款5-8折,MySQL 1年仅需19.9元.
云服务器ECS
云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的云计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。专业的售前技术支持,协助您选择最合适配置方案
点击查看->>> 弹性计算10年深厚技术积淀,技术领先、性能优异、 稳如磐石.
短信服务(Short Message Service)是广大企业客户快速触达手机用户所优选使用的通信能力。调用API或用群发助手,即可发送验证码、通知类和营销类短信;国内验证短信秒级触达,到达率最高可达99%;国际/港澳台短信覆盖200多个国家和地区,安全稳定,广受出海企业选用。
点击查看->>> 短信服务新用户 0元免费试用.

猜你喜欢

转载自blog.csdn.net/weixin_45681506/article/details/115016209