leetcode200-岛屿数量

原题

给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。

示例1:

输入:
11110
11010
11000
00000

输出: 1

示例2:

输入:
11000
11000
00100
00011

输出: 3

解法

思想

线性扫描整个二维网格,如果一个结点包含 1,则以其为根结点启动广度优先搜索,或深度优先搜索。搜索到的值设为 0 以标记访问过该结点,每经历过一次搜索说明岛的数量+1。直到整个二维数组都变成0。

代码

不嫌麻烦的可以建一个存储二维数组x、y位置的数据结构类。不然只能存储元素的偏移地址手动算x和y。

class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        int height = grid.length;
        if(height==0) return 0;
        int width = grid[0].length;
        Queue<Integer> queue = new LinkedList<Integer>();

        for(int i = 0;i<height;i++){
            for(int j = 0;j<width;j++){
                if(grid[i][j]=='1'){
                    grid[i][j] = '0';
                    queue.offer(i*width+j);
                    while(!queue.isEmpty()){
                        int index = queue.poll();
                        int index_x = index/width;
                        int index_y = index%width;
                        if(index_x!=height-1 && grid[index_x+1][index_y]=='1'){ 
                            queue.offer((index_x+1)*width+index_y);
                            grid[index_x+1][index_y] = '0';
                        }
                        if(index_x!=0 && grid[index_x-1][index_y]=='1'){ 
                            queue.offer((index_x-1)*width+index_y);
                            grid[index_x-1][index_y] = '0';
                        }
                        if(index_y!=0 && grid[index_x][index_y-1]=='1'){ 
                            queue.offer(index_x*width+index_y-1);
                            grid[index_x][index_y-1] = '0';
                        }
                        if(index_y!=width-1 && grid[index_x][index_y+1]=='1'){ 
                            queue.offer(index_x*width+index_y+1);
                            grid[index_x][index_y+1] = '0';
                        }
                    }
                    count++;
                }
            }
        }
        return count;
    }
}

在BFS的基础上改一下就成了DFS:

class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        int height = grid.length;
        if(height==0) return 0;
        int width = grid[0].length;
        Stack<Integer> stack = new Stack<>();

        for(int i = 0;i<height;i++){
            for(int j = 0;j<width;j++){
                if(grid[i][j]=='1'){
                    grid[i][j] = '0';
                    stack.push(i*width+j);
                    while(!stack.empty()){
                        int index = stack.peek();
                        int index_x = index/width;
                        int index_y = index%width;
                        if(index_x!=height-1 && grid[index_x+1][index_y]=='1'){ 
                            stack.push((index_x+1)*width+index_y);
                            grid[index_x+1][index_y] = '0';
                            continue;
                        }
                        if(index_x!=0 && grid[index_x-1][index_y]=='1'){ 
                            stack.push((index_x-1)*width+index_y);
                            grid[index_x-1][index_y] = '0';
                            continue;
                        }
                        if(index_y!=0 && grid[index_x][index_y-1]=='1'){ 
                            stack.push(index_x*width+index_y-1);
                            grid[index_x][index_y-1] = '0';
                            continue;
                        }
                        if(index_y!=width-1 && grid[index_x][index_y+1]=='1'){ 
                            stack.push(index_x*width+index_y+1);
                            grid[index_x][index_y+1] = '0';
                            continue;
                        }else{
                            stack.pop();
                        }
                    }
                    count++;
                }
            }
        }
        return count;
    }
}

当然DFS还可以用递归的系统调用栈:(作者:LeetCode)

class Solution {
  void dfs(char[][] grid, int r, int c) {
    int nr = grid.length;
    int nc = grid[0].length;

    if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
      return;
    }

    grid[r][c] = '0';
    dfs(grid, r - 1, c);
    dfs(grid, r + 1, c);
    dfs(grid, r, c - 1);
    dfs(grid, r, c + 1);
  }

  public int numIslands(char[][] grid) {
    if (grid == null || grid.length == 0) {
      return 0;
    }

    int nr = grid.length;
    int nc = grid[0].length;
    int num_islands = 0;
    for (int r = 0; r < nr; ++r) {
      for (int c = 0; c < nc; ++c) {
        if (grid[r][c] == '1') {
          ++num_islands;
          dfs(grid, r, c);
        }
      }
    }

    return num_islands;
  }
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode200-%e5%b2%9b%e5%b1%bf%e6%95%b0%e9%87%8f/

(0)
彭晨涛彭晨涛管理者
上一篇 2019年11月23日
下一篇 2019年11月25日

相关推荐

  • leetcode1-两数之和

    原题 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能…

    算法 2019年12月20日
    0110
  • leetcode76-最小覆盖子串

    原题 给你一个字符串 S、一个字符串 T,请在字符串 S 里面找出:包含 T 所有字符的最小子串。 示例: 输入: S = "ADOBECODEBANC", T = "ABC" 输…

    算法 2020年5月23日
    0170
  • leetcode80-删除排序数组中的重复项II

    原题 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外…

    算法 2020年5月26日
    0140
  • leetcode86-分隔链表

    原题 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。 你应当保留两个分区中每个节点的初始相对位置。 示例: 输入: head…

    算法 2020年4月27日
    0150
  • leetcode744-寻找比目标字母大的最小字母

    原题 给定一个只包含小写字母的有序数组letters 和一个目标字母 target,寻找有序数组里面比目标字母大的最小字母。 数组里字母的顺序是循环的。举个例子,如果目标字母tar…

    算法 2020年1月5日
    0210
  • leetcode111-二叉树的最小深度

    原题 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,nul…

    算法 2020年3月25日
    0220
  • leetcode198-打家劫舍

    原题 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会…

    算法 2020年5月29日
    060
  • leetcode221-最大正方形

    原题 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。 示例: 输入: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 …

    算法 2020年5月8日
    0110
  • leetcode41-缺失的第一个正数

    原题 给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3…

    算法 2020年5月11日
    0130
  • 程序员面试金典01.06-字符串压缩

    原题(来源Leetcode) 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字…

    算法 2020年3月16日
    0230

发表回复

登录后才能评论