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 ...
随机推荐
- Netty的ByteToMessageDecoder/LengthFieldBasedFrameDecoder
是个inbound handler,channelRead方法里面,用一个bytebuf(cumulation)来把下一个数据包和当前这一个拼在一起,以免同一个请求被拆包.然后callDecode,里 ...
- Python2和Python3安装注意事项
1. 到官网 https://www.python.org/downloads/windows/ 下载 Windows x86-64 executable installer版本: 2. python ...
- git 在某个分支下创建新分支
首先要强调一个观念,那就是在某个分支A下创建新的分支B,是指使用A分支下的代码,并不是A/B这样的层级结构. 比如,我想要在非主分支dev 下面创建子分支dev_dev >>>1.创 ...
- A Statistical Model for Scientific Readability-paper
Authors: Luo SiCarnegie Mellon University, Pittsburgh, PA Jamie CallanCarnegie Mellon University, Pi ...
- 数组排序自定义comparator()
案例1:现在有一个普通数组arr = [3,1,2,4,5,6,8,0,1]; 自定义一个排序方法: function createComparator(){ return function (obj ...
- how to tell gcc with c99 enable
just copy the make file here. CC = gccCFLAGS = -Wall -std=c99OUTFILE = outputfileOBJS = source.oSRCS ...
- oracle-taf
http://blog.sina.com.cn/s/blog_48567d850102wck0.html配置目标:把RAC系统配置为“主-备”模式,即平时所有连接都在rac01这个节点上,当rac01 ...
- 记录一次mysql查询速度慢造成CPU使用率很高情况
1.某日zabbix告警,某台机器CPU使用率过高. 查看慢查询日志,看到很多sql语句都超过10秒 把sql语句拿出来放在查询窗口执行.前面加上explain就可以查看详细查询信息 playcode ...
- docker基本命令使用
学会使用docker命令帮助 docker help 子命令 查看docker镜像 docker images 搜索镜像 docker search 镜像名 下载镜像 docker pull 镜像名 ...
- Spring Boot - 基础 POM 文件
表 1. Spring Boot 推荐的基础 POM 文件 名称 说明 spring-boot-starter 核心 POM,包含自动配置支持.日志库和对 YAML 配置文件的支持. spring-b ...