leetcode200
深度优先搜索,每次遇到1,则岛的数量+1,从这个1开始找到所有相连的1,将其改为0。
public class Solution
{
private void dfsSearch(char[,] grid, int i, int j, int rows, int cols)
{
if (i < || i >= rows || j < || j >= cols)
{
return;
}
if (grid[i, j] == '')
{
return;
}
grid[i, j] = '';
dfsSearch(grid, i + , j, rows, cols);
dfsSearch(grid, i - , j, rows, cols);
dfsSearch(grid, i, j + , rows, cols);
dfsSearch(grid, i, j - , rows, cols);
} public int NumIslands(char[,] grid)
{
var rows = grid.GetLength();
var cols = grid.GetLength();
if (rows == || cols == )
{
return ;
} int count = ;
for (int i = ; i < rows; i++)
{
for (int j = ; j < cols; j++)
{
if (grid[i, j] == '')
{
count++;
dfsSearch(grid, i, j, rows, cols);
}
}
}
return count;
}
}
leetcode200的更多相关文章
- Java 图的遍历-LeetCode200
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [Swift]LeetCode200.岛屿的个数 | 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 深度优先+广度优先】 岛屿数量
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- leetcode200 Number of Islands
""" Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- LeetCode200 岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- 并查集算法Union-Find的思想、实现以及应用
并查集算法,也叫Union-Find算法,主要用于解决图论中的动态连通性问题. Union-Find算法类 这里直接给出并查集算法类UnionFind.class,如下: /** * Union-Fi ...
随机推荐
- 关于Java的特点之多态
多态--概念 所谓多态,就是指一个引用(类型)在不同情况下的多种状态.也可以理解成:多态是指通过指向父类的指针,来调用在不同子类中实现的方法. 实现多态有两种方式:1.继承:2.接口 多态--注意事项 ...
- js实现上拉加载思路整理
1.整体模拟滚动 监听touchmove事件,比较 scrollTop 和 $scroller.scrollHeight() - $container.height(). 缺点:滑动不流畅, tran ...
- python 使用jinjia2 生成文件的空格处理
例如: <div> {% if True %} yay {% endif %} </div> 生成文件为 <div> yay </div> 两种方法解决 ...
- 使用element-ui 遇到的问题
Pagination 分页 在使用分页的时候,每次切换pageSize的时候,需要把current-page置为1 重新加载数据. 但是当current-page !== 1的时候,修改current ...
- 安装pitchpork 及 pacbioscience 的问题及解决
1. error while loading shared libraries: libpbbam.so: cannot open shared 解决: find -name libpbbam.so ...
- vim选中多行缩进(python多行缩进)与删除多行前面的空格
最近用vim写python,有时候会在一段代码前面套一个循环的操作,这个时候将这一段代码整体向后平移四个空格,来满足vim缩进的要求,如何做到这一点呢? 1. ESC之后,ctrl+v进入多行行首选中 ...
- 导入maven项目导入依赖不会报错,但使用的jar会标红
方法1: 1.通过编译找到报错的jar; 2.在 repository找到此jar,一般未下载完大小为1k我的是这样(); 3.删除未下载完全的jar,在项目上执行maven reimport会重新下 ...
- 2018.4.27 java容器
一.容器的概念 在Java当中,如果有一个类专门用来存放其它类的对象,这个类就叫做容器,或者就叫做集合,集合就是将若干性质相同或相近的类对象组合在一起而形成的一个整体 二.容器与数组的关系 之所以需要 ...
- some working learning总结学习(二)
2. 读取excel上的测试用例和结果,成json格式,最终存到access数据库 结果如下: { : { 'Req_ID': 'Fqqqqq/qqqqq', 'Case_ID': 'ATC_LvPw ...
- C++学习(三十九)(C语言部分)之 游戏项目(2048游戏)
/***************************项目 2048**********************c语言编写 图形库制作时间:2019.04.03 准备工具: vs2013 图形库 i ...