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. PythonStudy——Pycharm 小技巧

    分享Pycharm中一些不为人知的技巧 工欲善其事必先利其器,Pycharm 是最受欢迎的Python开发工具,它提供的功能非常强大,是构建大型项目的理想工具之一,如果能挖掘出里面实用技巧,能带来事半 ...

  2. C语言----管道

    一.管道的概念 管道是一种队列类型的数据结构,它的数据从一端输入,另一端输出.管道最常见的应用是连接两个进程的输入输出,即把一个进程的输出编程另一个进程的输入.shell中存在专门的管道运算符&quo ...

  3. Azure SQL 数据库仓库Data Warehouse (4) 2018 TechSummit 动手实验营

    <Windows Azure Platform 系列文章目录> 上传一下之前在2018 TechSummit的动手实验营:Azure数据仓库PaaS项目架构规划与实战入门 包含PPT和Wo ...

  4. win7文件夹带锁标志如何去除?win7去除文件夹带锁标志的方法

    win7文件夹带锁标志如何去除?win7去除文件夹带锁标志的方法 http://www.xitongcheng.com/jiaocheng/win7_article_30333.html 具体方法如下 ...

  5. TCP/IP学习20180630-数据链路层-router choose

    IP路由选择 当一个IP数据包准备好了的时候,IP数据包(或者说是路由器)是如何将数据包送到目的地的呢?它是怎么选择一个合适的路径来"送货"的呢? 最特殊的情况是目的主机和主机直连 ...

  6. kafka的log存储解析——topic的分区partition分段segment以及索引等(转发)

    原文 https://www.cnblogs.com/dorothychai/p/6181058.html 引言 Kafka中的Message是以topic为基本单位组织的,不同的topic之间是相互 ...

  7. windows copy 和xcopy

    #将文件夹下文件拷贝到指定目录 将d盘下1文件夹内所有内容拷贝到测试目录下copy d:\1\ Z:\chenkai\测试目录\ /yxcopy D:\soft\svn工具 Z:\chenkai\测试 ...

  8. ijkplayer之.so文件编译过程

    本文转载:ijkplayer编译so库真没那么难: 1.如何使用ijkplayer 官方:https://github.com/Bilibili/ijkplayer build.gradle添加下述依 ...

  9. “永恒之蓝”(Wannacry)蠕虫全球肆虐 安装补丁的方法

    “永恒之蓝”利用0day漏洞 ,通过445端口(文件共享)在内网进行蠕虫式感染传播,没有安装安全软件或及时更新系统补丁的其他内网用户就极有可能被动感染,所以目前感染用户主要集中在企业.高校等内网环境下 ...

  10. 04-体验一下apache组织封装的BeanUtil工具包

    apache 自己为程序员们封装了一个专门用于处理的工具类,其功能有(数据类型会自动转成与JavaBean相关的) map转javabean javabean转map javabean对象复制 获取j ...