[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 ...
随机推荐
- 关于element中修改组件使用深度选择器/deep/的问题
作为一个小白,在使用饿了么ui的时候,想改一下里面的组件属性,但是发现虽然在页面上能找到对应的标签,然而在代码里却没法找到,使用了两种方法来修改其中的默认样式 第一种,去除style标签里的scope ...
- Python中splitlines方法判断文本中一行结束除了回车换行符是否还有其他字符?
Python中splitlines([keepends])方法用于返回由原字符串中各行组成的列表,在行边界的位置拆分. 如果keepends=True,结果列表中包含行边界,否则不包含 行边界的字符. ...
- 爬取网页内容后写入文件报错UnicodeEncodeError: 'gbk' codec can't encode的问题解决方案
老猿使用如下代码读取网页内容: req = urllib.request.Request(url=url,headers=header) text = urllib.request.urlopen(r ...
- django 删除数据库表后重新同步的方法
python manage.py sqlmigrate your_app_name 0001 把your_app_name换成自己的app名字即可看到框架自动生成的创建表sql语句,于是我就这样看到了 ...
- 科大讯飞语音合成系统 V5.0绿色便携版
中文名: 中科大讯飞Interphonic 5.0语音合成系统英文名: Interphonic 5.0版本: 5.0发行时间: 2006年制作发行: 中科大讯飞语言: 简体中文系统简介InterPho ...
- Nodepad++ 没有TextFX Characters插件
Nodepad++ 没有TextFX Characters插件 原因:从6.5以上的某个版本开始,就没有了默认的这个插件. 我试了最新的版本下载插件也不行,只好下载一个6.4.5的版本,再更新插件管理 ...
- 题解-SHOI2005 树的双中心
SHOI2005 树的双中心 给树 \(T=(V,E)(|V|=n)\),树高为 \(h\),\(w_u(u\in V)\).求 \(x\in V,y\in V:\left(\sum_{u\in V} ...
- Android Studio/IDEA插件
1.android parcelable code generator 2.android code generator3.gson format4.android postfix completio ...
- 世界上最快的排序算法——Timsort
前言 经过60多年的发展,科学家和工程师们发明了很多排序算法,有基本的插入算法,也有相对高效的归并排序算法等,他们各有各的特点,比如归并排序性能稳定.堆排序空间消耗小等等.但是这些算法也有自己的局限性 ...
- C++ 虚函数表与多态 —— 多态的简单用法
首先看下边的代码,先创建一个父类,然后在来一个继承父类的子类,两个类中都有自己的 play() 方法,在代码的第35-37行,创建一个父类指针,然后将子类地址引用赋值给父类,这时调用 P 指针的 pl ...