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 ...
随机推荐
- 解决android 9上无法使用http协议
用户反应本来好用的app,突然无法访问服务器,不能正常用了,拿到手机,从头检查权限,重新安装都不能解决,网络是正常的,怎么就不能访问网络了呢?所有想到的办法都用了而不能解决,最后想起看一下androi ...
- SpringBoot的学习【4.快速实现一个SpringBoo的应用】
1.引子 正常创建一个 Spring Boot 应用的顺序: 创建 Maven 项目 pom 文件导入依赖(参照 Spring 官方文档) 编写主程序 编写业务逻辑 但其实IDE( idea 和 Sp ...
- web(七)css的语法规则、注释
css的语法规则:特殊的css语法标识. !important:当使用多种方式设定标签样式时,设定样式渲染的应用优先权,声明在取值之后. .important { color: red !import ...
- JAVA常用设计模式(一、单例模式、工厂模式)
JAVA设计模式之单例模式 import java.util.HashMap; import java.util.Map; /** * 设计模式之单例模式 * 单例模式(Singleton Patte ...
- LINUX文件删除,但磁盘空间未释放
最近在进行系统压测,由于服务器节点太多,便写了个简单的脚本,在执行过程中发现,日志文件删除后,磁盘空间只释放了一小部分,任有大部分磁盘空间未释放. 使用lsof | grep delete命令,发现已 ...
- for break
public static void main(String[] args) { aaa: for (int j = 0; j < 2; j++) { System.out.println(&q ...
- Java 初始 多态
什么是多态 简单的来说就是具有多种形态的能力的特征 package ten; public interface Day1 { public void ring(); } package ten; pu ...
- Javascript 将一个句子中的单词首字母转成大写
Javascript 将一个句子中的单词首字母转成大写 先上代码 function titleCase(str) { str = str.toLowerCase().split(" &quo ...
- C# 左右补零
//不够4位补零 public static string addZero(int val) { string str = val + ""; int strLen = str.L ...
- 如何在hanlp词典中手动添加未登录词
我们在使用hanlp词典进行分词的时候,难免会出现分词不准确的情况,原因是由于内置词典中并没有收录当前的这个词,也就是我们所说的未登录词,只要把这个词加入到内置词典中就可以解决类似问题,如何操作,下 ...