[leetcode]200. Number of Islands岛屿数量
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岛屿数量的更多相关文章
- 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 ...
- [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 ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [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 ...
- 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 用了第一种方式, ...
- [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 ...
- 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 ...
- (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 ...
- 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 ...
随机推荐
- UPX使用教程
UPX是一个通用可执行文件压缩器,由于其具有: 压缩率高:压缩效果优于zip/gzip: 解压速度快:在奔腾133上即可达到大约10MB/秒: 压缩的可执行文件没有额外的内存开销: 安全:可以列表,检 ...
- Jmeter代理服务器录制脚本--浏览器拦截访问链接
在 Jmeter性能测试的过程中您是否会遇到代理服务器无法打开浏览器,无法录制脚本的情况呢? 在测试过程中,我也遇到过这样的问题,希望能帮到正在找寻答案的你.... Jmeter录制脚本时,跟http ...
- Apache Flink 如何正确处理实时计算场景中的乱序数据
一.流式计算的未来 在谷歌发表了 GFS.BigTable.Google MapReduce 三篇论文后,大数据技术真正有了第一次飞跃,Hadoop 生态系统逐渐发展起来. Hadoop 在处理大批量 ...
- 快速理解Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容的区别
<Python中使用百分号占位符的字符串格式化方法中%s和%r的输出内容有何不同?>老猿介绍了二者的区别,为了快速理解,老猿在此使用另外一种方式补充说明一下: 1.使用%r是调用objec ...
- PyQt(Python+Qt)学习随笔:QListView的movement属性
老猿Python博文目录 老猿Python博客地址 QListView的movement属性用于控制在视图中怎么移动数据项,其类型为枚举类型QListView.Movement,有如下取值: Stat ...
- Microsoft工具之Disk2vhd
Official documents:https://docs.microsoft.com/zh-cn/sysinternals/downloads/disk2vhd 1.Introduction D ...
- C++中对一个布尔类型的变量按位取反结果不变
C++中对一个bool类型的变量按位取反是无效的.例如: bool a = true; bool b = ~a; // b的值还是true
- Oracle表操作-创建及增删改查
数据类型: 1.CHAR:定长字符类型,默认长度是1,最长不超过2000字节. 2.CARCHAR2(length):可变字符类型,默认长度是1,最长不超过4000字符. 3.NUMBER(P,S): ...
- web安全~文件包含总结
文章来自freebuf,作者总结的很好,所以拿来做笔记用!!! 0×01 文件包含简介 服务器执行PHP文件时,可以通过文件包含函数加载另一个文件中的PHP代码,并且当PHP来执行,这会为开发者节省大 ...
- [SUCTF 2019]Game
buuoj杂项复现 下载了之后给了我们一张图片了网站的源代码 图片简单分析了之后没有什么内容,先看源代码的index.html 里面有base32编码,解码 ON2WG5DGPNUECSDBNBQV6 ...