dfs的第一题

被边界和0包围的1才是岛屿,问题就是分理出连续的1

思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿。

/*
思路是:遍历二维数组,遇到1就把周围连续的1变成0,res+1,然后继续遍历,直到结束
周围连续1置零用的是dfs,向四个位置搜索,遇到0返回
*/
public int numIslands(char[][] grid) {
if (grid.length==0) return 0;
int r = grid.length;
int c = grid[0].length;
int res = 0;
//遍历数组
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (grid[i][j]=='1')
{
res++;
//进行置零操作
dfs(grid,i,j,r,c);
}
}
}
return res;
}
public void dfs(char[][] grid,int i,int j,int r,int c)
{
//出界和遇0返回
if (i<0||j<0||i>=r||j>=c||grid[i][j]!='1') return;
//置零
grid[i][j]='0';
//把周围连续的置零
dfs(grid,i-1,j,r,c);
dfs(grid,i+1,j,r,c);
dfs(grid,i,j-1,r,c);
dfs(grid,i,j+1,r,c);
}

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

  1. 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 ...

  2. [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 ...

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

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

  4. [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 ...

  5. 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 用了第一种方式, ...

  6. [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 ...

  7. 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 ...

  8. (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 ...

  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. Alpha冲刺-第六次冲刺笔记

    Alpha冲刺-冲刺笔记 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs. ...

  2. Django 2版本

    django2.0里面的path第一个参数不支持正则,你写什么就匹配,100%精准匹配 django2.0里面的re_path对应着django1.0里面的url 虽然django2.0里面的path ...

  3. 转:【Python3网络爬虫开发实战】 requests基本用法

    1. 准备工作 在开始之前,请确保已经正确安装好了requests库.如果没有安装,可以参考1.2.1节安装. 2. 实例引入 urllib库中的urlopen()方法实际上是以GET方式请求网页,而 ...

  4. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中使用sortItems进行项排序

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 树型部件QTreeWidget中的项可以使用sortItems方法按照指定列进行排序,调用语法: s ...

  5. PyQt(Python+Qt)学习随笔:Qt Designer中QAbstractButton派生按钮部件的shortcut 属性

    shortcut 属性保存与按钮关联的快捷键.可以使用shortcut()和setShortcut(QKeySequence)访问和设置该属性. 关于这个属性官网介绍的不多,经老猿实际验证,它与tex ...

  6. Redis整合MySQL和MyCAT分库组件(来源是我的新书)

    MyCAT是一个开源的分布式数据库组件,在项目里,一般用这个组件实现针对数据库的分库分表功能,从而提升对数据表,尤其是大数据库表的访问性能.而且在实际项目里,MyCAT分库分表组件一般会和MySQL以 ...

  7. 深入解析volatile关键字

    前言 很高兴遇见你~ 欢迎阅读我的文章. volatile关键字在Java多线程编程编程中起的作用是很大的,合理使用可以减少很多的线程安全问题.但其实可以发现使用这个关键字的开发者其实很少,包括我自己 ...

  8. 题解-Koishi Loves Construction

    题解-Koishi Loves Construction 前缀知识 质数 逆元 暴搜 Koishi Loves Construction 给定 \(X\),\(T\) 组测试数据,每次给一个 \(n\ ...

  9. deepstrem编译缺少gst/gst.h解决方案

    Deepstream在编译程序的程序的显示缺少gst/gst.h 具体情况是Deepstream运行已编译好的deepstream-app可以正常运行,但对源码编译的时候出现以述情况,初步分析是我们安 ...

  10. 部署在GitHub的个人博客如何绑定个人域名

    前提是已经搭建好了自己的个人博客 如果想要搭建自己的个人博客可以来我的个人博客学习呀 地址 购买域名 首先想要绑定域名,总归需要去购买一个属于自己的域名吧,我是在腾讯云上面购买的域名(不是广告) 在腾 ...