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 ...
随机推荐
- numpy.where() 用法详解
numpy.where (condition[, x, y]) numpy.where() 有两种用法: 1. np.where(condition, x, y) 满足条件(condition),输出 ...
- mysql错误集合
一.This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法 这是我们开启了bin-log ...
- go web framework gin 启动流程分析
最主要的package : gin 最主要的struct: Engine Engine 是整个framework的实例,它包含了muxer, middleware, configuration set ...
- 全文检索的Demo
用到lucene版本为6.3.0版本,利用的分词器为IKAnalyzer分词器,该分词对中文有较好的支持.关于支持lucene的6.xx以上的IkAnalyzer分词jar包下载地址:https:// ...
- CSS 隐藏ul
老师要求只能用css做出覆盖ul时显示ul,之前一直在试 [ display:none display:block ]后来发现一直是闪烁的状态 不能实现消失 然后就试用了透明度,一下就可以了 ...
- Markdown最常用的几个语法
更多语法请参考:Markdown语法说明 http://wowubuntu.com/markdown/
- opencv读取摄像头实时流代码
opencv读取摄像头实时流代码: #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; ...
- (26)基于cookie的登陆认证(写入cookie、删除cookie、登陆后所有域下的网页都可访问、登陆成功跳转至用户开始访问的页面、使用装饰器完成所有页面的登陆认证)
获取cookie request.COOKIES['key'] request.get_signed_cookie(key, default=RAISE_ERROR, salt='', max_age ...
- findHomography(src_points, dst_points, CV_RANSAC)
Homography,即单应性,该函数用于求src_points转换为dst_poinsts的单应性矩阵: 为了理解单应性,必须先引入透视变换的概念:把空间坐标系中的三维物体或对象转变为二维图像表示的 ...
- POJ3017 Cut the Sequence
题意 Language:Default Cut the Sequence Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 122 ...