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.

  1. Input:
  2. 11110
  3. 11010
  4. 11000
  5. 00000
  6.  
  7. Output: 1
  1. Input:
  2. 11000
  3. 11000
  4. 00100
  5. 00011
  6.  
  7. Output: 3

思路:

- DFS and flip the bit-1 on the grid to 0 as we go: to 4 different directions
- Loop through all positions
- Visited spots won't be visited again because they are updated to '0'

怎样check 当前这格是否被visited过?

1.开一个boolean[][]  sized as the original input matrix.  但需要额外空间

2.directly flag ‘2’ or ‘0’ (any char excpet ‘1’) in the original input matrix  插个小旗flag一下

相关小岛题变种

[leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

代码:

  1. class Solution {
  2. public int numIslands(char[][] grid) {
  3. if ( (grid.length==0) || (grid[0].length==0) ) return 0;
  4. int count =0;
  5. for (int i = 0; i<grid.length;i++) {
  6. for (int j=0;j<grid[i].length;j++) {
  7. if (grid[i][j]=='1') {
  8. count++;
  9. helper(grid, i, j);
  10. }
  11. }
  12. }
  13.  
  14. return count;
  15. }
  16.  
  17. private void helper(char[][] grid, int i, int j) {
  18. if ((i<0) || (j<0) || (i>=grid.length) || (j>=grid[0].length)
  19. || grid[i][j] != '1' ) return;
  20.  
  21. grid[i][j] = '0'; // mark visited spot
  22. helper(grid, i+1, j);
  23. helper(grid, i-1, j);
  24. helper(grid, i, j+1);
  25. helper(grid, i, j-1);
  26. }
  27. }

[leetcode]200. Number of Islands岛屿个数的更多相关文章

  1. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  2. LeetCode 200. Number of Islands 岛屿数量(C++/Java)

    题目: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is s ...

  3. [leetcode]200. Number of Islands岛屿数量

    dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...

  4. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  5. [LeetCode] 0200. Number of Islands 岛屿的个数

    题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...

  6. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  7. (BFS/DFS) leetcode 200. Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. Leetcode 200. number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

随机推荐

  1. redis hset hmset过期时间

    hmset m k v > hset m k v (integer) > hget m k "v" > expire m (integer) > ttl m ...

  2. [转]Java泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  3. DevExpress GridView 整理(转)

    DevExpress GridView 那些事儿 1:去除 GridView 头上的 "Drag a column header here to group by that column&q ...

  4. 【C++】读取参数的类

    在C++程序中,如果我们把程序中的参数都保存在txt文本中,运行时再去读取.这样的好处是,当我们需要调参的时候,不需要每次都重新编译程序,大大提升了效率. 今日分享一份实现以上功能的代码,代码来源:h ...

  5. 黄聪:微信URL Scheme,URL唤起微信

    微信URL Scheme 在外部浏览器中,可以通过<a href="weixin://">打开微信APP 也可以通过加一些参数,打开微信APP里的指定页面 <a ...

  6. Linux 入侵检测小结

    Linux  入侵检测小结 0x00 审计命令 在linux中有5个用于审计的命令: last:这个命令可用于查看我们系统的成功登录.关机.重启等情况:这个命令就是将/var/log/wtmp文件格式 ...

  7. vmware中的linux虚拟机配置以nat模式上网,并用xshell连接该虚拟机

    1.  首先确保宿主机上的vmnet8处于启用状态 2.  以管理员身份运行vmware >> 编辑 >> 虚拟机网络编辑器 >> 选中Vmnet8 >> ...

  8. MySQL 大数据备份方案之Percona XtraBackup

    Xtrabackup介绍 1.Xtrabackup是什么 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写),是商业备份工具InnoDB Hotbacku ...

  9. Hibernate多对一ManytoOne

    ------------------------Hibernate多对一ManytoOne 要点: ManytoOne配置在多端 可以配置级联操作 @ManyToOne(cascade=Cascade ...

  10. Action Form的过程

    1.读取配置(初始化ModuleConfig对象) Struts框架总控制器(ActionServlet)是一个Servlet, 在web.xml中配置成自动启动的Servlet. 读取配置文件(st ...