[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 ...
随机推荐
- gradle插件版本号和Gradle版本号对应关系
Plugin version Required Gradle version 1.0.0 - 1.1.3 2.2.1 - 2.3 1.2.0 - 1.3.1 2.2.1 - 2.9 1.5.0 2.2 ...
- 等待多线程完成的CountDownLatch(带示例)
开始磨刀霍霍向多线程了,这期是 CountDownLatch 的一个小示例. 定义:CountDownLatch 允许一个或多个线程等待其他线程完成操作. 应用需求举例:假设有4个线程,A.B.C.D ...
- day1(ModelViewSet序列化限流排序)
1.DRF初始化 1.认证 2.权限 3.限流 4.序列化 5.分页 6.版本 7.过滤 8.排序 1.1安装DjangoRestFramework pip install djangoresfra ...
- Django-DRF框架
一.RESTfull设计风格 1.什么是RESTfull? 1)REST:即Representational State Transfer的缩写.维基百科称其为"具象状态传输",国 ...
- PyQt(Python+Qt)学习随笔:QListWidget获取当前选中项的selectedItems方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListWidget的selectedItems方法返回列表部件中所有选中项的一个列表,调用语法如 ...
- PyQt(Python+Qt)学习随笔:QTreeView树形视图的wordWrap属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的wordWrap属性用于控制视图展示数据项文本的单词换行原则,如果该值为 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中部件的快捷菜单策略(contextMenuPolicy)取值及含义
在Qt Designer中可以设置部件的快捷菜单策略,快捷菜单通过在部件上点击鼠标右键触发. 快捷菜单策略通过枚举类型Qt.ContextMenuPolicy来定义,对应枚举类型取值及含义如下: 通过 ...
- 连接数从异常到 300 到 5(RDS MySQL 的一个大坑•后记)
在 <记 RDS MySQL 的一个大坑> 中,我提到遇到 User juxxxxxxxxxx already has more than 'max_user_connections' a ...
- 手写线程池,对照学习ThreadPoolExecutor线程池实现原理!
作者:小傅哥 博客:https://bugstack.cn Github:https://github.com/fuzhengwei/CodeGuide/wiki 沉淀.分享.成长,让自己和他人都能有 ...
- Panda Global获悉,美国承诺4年内明确区块链数字资产监管方式!
近日,美国商品期货交易委员会(CFTC)宣布,在4年内将会全面把加密货币监管列为优先事项.Panda Global从7月8日公布的新战略中获悉,此次CFTC公布了自己接下来的新框架,并且在框架中承诺: ...