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.

Input:
11110
11010
11000
00000 Output: 1
Input:
11000
11000
00100
00011 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你究竟有几个异小岛?

代码:

class Solution {
public int numIslands(char[][] grid) {
if ( (grid.length==0) || (grid[0].length==0) ) return 0;
int count =0;
for (int i = 0; i<grid.length;i++) {
for (int j=0;j<grid[i].length;j++) {
if (grid[i][j]=='1') {
count++;
helper(grid, i, j);
}
}
} return count;
} private void helper(char[][] grid, int i, int j) {
if ((i<0) || (j<0) || (i>=grid.length) || (j>=grid[0].length)
|| grid[i][j] != '1' ) return; grid[i][j] = '0'; // mark visited spot
helper(grid, i+1, j);
helper(grid, i-1, j);
helper(grid, i, j+1);
helper(grid, i, j-1);
}
}

[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. python_字符编码&格式化

    电脑最小储存单位是bit(位),8bit为一个Byte(字节), 8bit=1Byte 1024Byte=1KB 1024KB=1MB 1024MB=1GB 1024GB=1TB 编码的故事: 计算机 ...

  2. H3C交换机限制子网之间的相互访问

    acl number 3000     rule 1 permit ip source 10.0.5.0 0.0.0.255 destination 172.16.1.100 0   #允许10.0. ...

  3. ILBC 运行时 (ILBC Runtime) 架构

    本文是 VMBC / D# 项目 的 系列文章, 有关 VMBC / D# , 见 <我发起并创立了一个 VMBC 的 子项目 D#>(以下简称 <D#>)  https:// ...

  4. Hbase 与Hive整合

    HBase与Hive的对比 25.1.Hive 25.1.1.数据仓库 Hive的本质其实就相当于将HDFS中已经存储的文件在Mysql中做了一个双射关系,以方便使用HQL去管理查询. 25.1.2. ...

  5. 基于MATLAB System Generator 搭建Display Enhancement模型

    基于MATLAB System Generator 搭建Display Enhancement模型

  6. 阿里四不像Fourinone

    阿里四不像——分布式核心技术框架 Fourinone https://blog.csdn.net/shero_zsmj/article/details/52277194

  7. awk字符串操作(字符串链接、传入传出shell变量)

    1.awk基础 awk的环境变量及其意义   https://blog.csdn.net/snowpay/article/details/52451718 linux awk命令详解 https:// ...

  8. 7、Curator的常规操作

    package com.ourteam; import org.apache.curator.RetryPolicy;import org.apache.curator.framework.Curat ...

  9. html/css/js-如何利用jq来更改属性的值和获取属性的值

    jquery的使用在web开发中是非常广泛的,虽然说比较容易,易学,但在开发过程中,也总是会碰到各种各样的小问题. 我曾经就遇到这种问题,jq如何获取属性值和更改属性值的. 众所周知,attr()可以 ...

  10. centos7将可执行程序做成服务

    1.systemctl 用法:systemctl [OPT] COMMAND [NAME]…启动服务:systemctl start NAME.service停止服务:systemctl stop N ...